How to bring information by clicking the dataGridView C #, and adding values from a column

0

Hello I'm having trouble creating a query from a gridview I used entity framework Well, I have a gridView it loads the Data normally

In the TextBox below I want to get the name of the person that is selected put in the field and in the grid where the number of pages I want to add the total and put in the textBox Total impression / month referring to the name selected in the field This is the code that loads GridView:

private void ler_Impressora()
{
        ImpressoraEntities context = new ImpressoraEntities();
        IEnumerable<Tmp_Printlog> lista = from p in context.Tmp_Printlog select p;
        dgvLista.DataSource = lista.ToList();
}
    
asked by anonymous 05.06.2017 / 18:02

1 answer

0

Just use the CellClick or CellContentClick event or any other dataGridView click event.

 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
 {
        int Total = 0;
        var Row = dataGridView1.CurrentRow;

        txtUsuario.Text = Row.Cells[1].Value.ToString();

        foreach (DataGridViewRow linha in dataGridView1.Rows)
        {
            if (linha.Cells[2].Value.ToString() == txtUsuario.Text)
            {
                Total += int.Parse(linha.Cells[1].Value.ToString());
            }
        }


        txtTotal.Text = Total.ToString();
 }
  • In the Row variable I picked up the selected line.
  • Then I got the username.
  • And after that I went through the entire DataGridView comparing the selected user with the user of the dataGridView, and added the printed pages.
  • And finally put the sum in the total TextBox.
21.06.2017 / 16:18