Searching Data with DataGridView

1

I'm working with WinForms and I'm trying to implement a DataGridView(dgv) in a search form that I have in my project.

What happens is: I have two dgvs that form the kind of master-detail. I did this by following this tutorial . The way it is in the tuto, I made some adjustments and everything worked fine, it returns me the bank data and such.

But I need to go a little further. I need in addition to the dgv to show the data of the bank, I also want to make that when clicking on a line it loads that data in a form of cadastre so that I can edit this data. I know that if I do in ListView it will work, but I also want to learn how to use dgv.

What's more, I have a search textbox and two radiobuttons, which are the search criteria for textbox , and one more button. That is, I choose the criterion in the radiobuttons (code or name) and put the value in the textbox and when I press the button, it searches the database and returns me in a dgv ... But this part of the search does not work and nor the doubleclick on the line, returning me an error that the index can not be negative.

Anyway, can anyone help me? Is it a link to a tutorial or example code?

The codes I have are:

public partial class frmPesquisaAluno : Form
{
    //decalramos a variável publica do tipo string
    public string sCdCodigo;
    public string sDsNome;

    public frmPesquisaAluno()
    {
        InitializeComponent();
        CarregarDados();
        //inicializamos a variável como vazia
        sCdCodigo = string.Empty;
        sDsNome = string.Empty;
    }

    private void frmPesquisaAluno_Load(object sender, EventArgs e)
    {

    }

    public DataViewManager dvManager;

    public void CarregarDados()
    {
        string strConexao = @"Data Source=ServidorBD;Initial Catalog=BD;Integrated Security=True;

        using (SqlConnection objConexao = new SqlConnection(strConexao))
        {
            DataSet ds = new DataSet("AlunosOcorrencia");

            SqlDataAdapter daCustomers = new SqlDataAdapter("SELECT * FROM Alunoes", objConexao);
            daCustomers.TableMappings.Add("Table", "Alunoes");
            daCustomers.Fill(ds);

            SqlDataAdapter daOrders = new SqlDataAdapter("SELECT * FROM Ocorrencias", objConexao);
            daOrders.TableMappings.Add("Table", "Ocorrencias");
            daOrders.Fill(ds);

            DataRelation relCustOrder;
            DataColumn colMaster1;
            DataColumn colDetail1;
            colMaster1 = ds.Tables["Alunoes"].Columns["AlunoID"];
            colDetail1 = ds.Tables["Ocorrencias"].Columns["AlunoID"];
            relCustOrder = new DataRelation("AlunoCorrencias", colMaster1, colDetail1);
            ds.Relations.Add(relCustOrder);

            dvManager = ds.DefaultViewManager;

            dataGridViewAlunos.DataSource = dvManager;
            dataGridViewAlunos.DataMember = "Alunoes";

            dataGridViewOcorrencias.DataSource = dvManager;
            dataGridViewOcorrencias.DataMember = "Alunoes.AlunoCorrencias";
        }
    }

    private void dataGridViewAlunos_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            sCdCodigo = dataGridViewAlunos.SelectedRows[0].ToString();
            sDsNome = dataGridViewAlunos.SelectedRows[0].ToString();
        }
        catch (Exception)
        {

            throw;
        }
    }

    private void dataGridViewAlunos_DoubleClick(object sender, EventArgs e)
    {
        try
        {
            sCdCodigo = dataGridViewAlunos.SelectedRows[0].ToString();
            sDsNome = dataGridViewAlunos.SelectedRows[0].ToString();
            Close();
            DialogResult = DialogResult.OK;
        }
        catch (Exception)
        {

            throw;
        }
    }

    public virtual void Pesquisar()
    {

    }

    private void buttonPesquisar_Click(object sender, EventArgs e)
    {
        //chamo o método de pesquisa
        Pesquisar();            
    }

    private void radioButtonCodigo_CheckedChanged(object sender, EventArgs e)
    {
        //quando o usuário clicar no RadioButton, o foco é 
        //automaticamente setado para o TextBox de pesquisa
        textBoxPesquisar.Focus();
    }

    private void radioButtonDescricao_CheckedChanged(object sender, EventArgs e)
    {
        //quando o usuário clicar no RadioButton, o foco é 
        //automaticamente setado para o TextBox de pesquisa
        textBoxPesquisar.Focus();
    }       
}

This is the complete code

    
asked by anonymous 01.05.2015 / 20:48

1 answer

0

Érik What is happening is that you are not passing the correct value to the next table where it contains the data that will be displayed in the details.

In case the Ocorrencias table has to have a search item for the selected student, your search will look like this:

SqlDataAdapter daOrders = new SqlDataAdapter("SELECT * FROM Ocorrencias WHERE idAlunao=@idAlunao", objConexao);

Check out the syntax!
Indicate the parameter in the search, so you can drill down into the occurrences of the item in the Students table. In order to better control the data you use in searches, look for the fields that you will use, so you can improve your query.

    
04.05.2015 / 15:18