Skip to content

Get the value from datagrid to textbox using c#

Ankush

Asked Mar 24, 2016, 1:42 AM EDT4 answers
Share
LinkedInX
I am developing a window application in which I have a datagridview. I get the data in datagridview from database and have added checkbox column in datagridview. when the checkbox is checked the value should be shown in label, for that I have written foreach loop and its working but,
when the user checks the first row the value is not refreshed, it gets refreshed when focus from first row is leaved.
 
Which is the appropriate datagridview event ??
 
The code  I have written
  1. Amt = 0;  
  2. foreach (DataGridViewRow dgr in dgvDetails.Rows)  
  3.    {  
  4.       if (Convert.ToBoolean(dgr.Cells[0].Value) == true)  
  5.       {  
  6.          Amt += Convert.ToInt32(dgr.Cells[12].Value.ToString());  
  7.       }  
  8.       txtCash_Amt.Text = Amt.ToString(); 
  9.    }   
 

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Surya Kant
    0
    As per your Question you can write this code under checkbox clicked event or the td where checkbox present under that td clicked event.
     
    Hopes this work for you.
    0
  • 0
    The foreach loop I have written works, but when, i.e. when the current row in datagridview is changed (from first row to second row)
    0
  • 0
    I solved the query.
     
    Write the following code in Datagridview  CellContentClick event.
     
    Code
    if (e.ColumnIndex == dgvDetails.Columns[0].Index)
                {
                    dgvDetails.EndEdit();
                    //the for loop i have written
                }
     
    0
  • 1 short reply
    • Surya Kant
      +1
      Grt Job... :)
      +1