1

i had a gridview which has 2 columns , one is textbox column and other is checkbox column, how to know which checkbox is checked .

enter image description here

As shown in image ,suppose any of the checkbox is checked , i want to display that the corresponding text box value to that checkbox.

can anyone help me?i tried the below code , but problem which i am facing is that , the values is getting displayed once i clicked to next checkbox then the previously checked checkbox values is getting displayed..

dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);

  void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {  
        object tempObj = dataGridView1.Rows[e.RowIndex].Cells[1].Value;
         dataGridView1_CurrentCellDirtyStateChanged(sender, e);

        if (((e.ColumnIndex) == 1) && ((bool)dataGridView1.Rows[e.RowIndex].Cells[1].Value))
        {
            MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());

        }
    }
Deadlock
  • 330
  • 1
  • 3
  • 21
  • 1
    What did you tried to do? I afraid that time for writing a question here will be same if you tried to search from internet: [datagridview.CellValueChanged event](http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx) or [datagridview.CellEndEdit event](http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx) – Fabio Aug 26 '13 at 08:29
  • Just look at the value of the given cell and bear in mind that, for checkboxes, there are only two possible values: true or false. – varocarbas Aug 26 '13 at 08:38
  • @varocarbas i agree with your comment but as intially the checkbox is not checked so checking true and false it return false everytime – Deadlock Aug 26 '13 at 08:43
  • ?! If all the checkboxes are unchecked, you would get every time false (what do you expect? C# lying to you? LOL). This is the whole point: knowing if they are unchecked (= false) or checked (= true). If you want to change its behaviour you can follow the same rules, that is: Cell.Value = True (checks it) and Cell.Value = False (unchecks it). – varocarbas Aug 26 '13 at 08:54

4 Answers4

3
 private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        if (dataGridView1.IsCurrentCellDirty)
        {
            dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    }

    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if (((e.ColumnIndex) == 1) && ((bool)dataGridView1.Rows[e.RowIndex].Cells[1].Value))
        {
            MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());

        }
    }

these below links helped me to understand the concept of cellvalue_changed and cell_content_click.. http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx

and by the help of these links i finally got the solution to my problem

Deadlock
  • 330
  • 1
  • 3
  • 21
1

it is as simple as this

//replace the row number and column name with your own 
if ((bool)dataGridView1.Rows[0].Cells["Column1"].Value)
 {
       //do your work
 }
Ehsan
  • 31,833
  • 6
  • 56
  • 65
  • it giving nullReference exception saying "object is not set to reference of an object" – Deadlock Aug 26 '13 at 08:41
  • have you changed the datagridview name? row number? and column name? If yes, does the row that you are specifying actually contain a record? have you initialized your datagrid? There are so many things that can go wrong. Show your code. – Ehsan Aug 26 '13 at 08:43
0
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex > -1 && e.ColumnIndex > -1)
    label1.Text = dataGridView1.Rows[e.RowIndex].Cells["Col1"].Value.ToString();
}
Nisha
  • 1,379
  • 16
  • 28
  • i want windows form solution not asp.net solutions... and why i need button ..i just want when i click the checkbox the text corresponding to that should get displayed. – Deadlock Aug 26 '13 at 09:23
0
private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == *someIndex*)
    {
        DataGridViewCheckBoxCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;
        if (cell != null)
        {
            if (cell.EditingCellValueChanged)
            {
                //CheckBox has been clicked
            }

            //here how to get the checkBoxCell value
            var cellChecked = cell.EditingCellFormattedValue;
        }
    }
}
Z.R.T.
  • 1,543
  • 12
  • 15