Data Search with Datagridview

Asked

Viewed 1,834 times

1

I’m working with Winforms and I’m trying to implement a DataGridView(dgv) on a research form I have in my project.

What happens is: I have two dgvs that form the sort of master-detail. I did it by following this tutorial. The way it is on Uto, I made some adaptations and everything worked out fine, it returns me the data of the database and such.

But I need to go a little further. I need that in addition to dgv showing the database data, I also want to make sure that by clicking on a line it loads this data into a registration form so that I can edit this data. I know if I do in ListView it will work, but I also want to learn how to use dgv.

What’s more, I have a research textbox and two radiobuttons, which are the search criteria for textbox, and one more button. That is, I choose the criteria in radiobuttons(code or name) and put the value in the textbox and at the push of the button, it do the search in the bank and return me in a dgv... But this part of the search does not work and neither Doubleclick on the line, returning me an error that Indice can not be negative.

Anyway can anyone help me ? Be a tutorial link or with sample codes ?

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

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 Occurrences 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!
Enter the parameter in the query, so you can detail the occurrences of the Alunoes table item.
To better control the data you use in the searches, try to put the fields you will use, so you improve your query.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.