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

Asked

Viewed 798 times

1

Hello I’m having difficulty creating a query of a gridview used Entity Framework Well, I have a gridView she carries the data normally inserir a descrição da imagem aqui

In the Textbox below I want to take the name of the person that this selected put in the field and grid where ta the number of pages I want to add the total and put in the textbox Total print/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();
}
  • WHEN do you want that information to appear? When do you click a cell on gridview? I suggest that that list be available in the class so you can search to add the information you want.

  • Hello, I want to pick the field that is selected or was clicked put the user name in the user textbox and the total print textbox put the total print sum(pages) that he made.

1 answer

1

Just use the event Cellclick or Cellcontentclick or any other 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();
 }
  • On the variable Row I caught the selected line.
  • Then I took the user’s name.
  • And after that I went through the whole dataGridView comparing the selected user with the user of dataGridView, and I added the printed pages.
  • And finally I put the sum in the total Textbox.

Browser other questions tagged

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