Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am Using a checkbox column in datagrid view.
On checking the checkbox of a particular row I want to set the back color of that row to different color and on uncheck, back to white.
On which event of datagrid should I write the code?

And one more thing how to get value of checked state of checkbox i.e. whether it is checked or not (true or false) in any event of datagrid?

Hoping for a solution.
Posted
Updated 15-Dec-10 21:41pm
v3
Comments
Abdul Quader Mamun 15-Dec-10 10:51am    
Spelling Check.
Dalek Dave 16-Dec-10 3:42am    
Edited for Grammar and Readability.

Hi Manoj,

On cell content click.

C#
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                if (e.ColumnIndex != 1)
                {
                    if (dataGridView1.Columns[e.ColumnIndex].Name = "colCheckBox")
                    {
                        if ( Convert.ToBoolean (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == true )
                        {
                            //do this
                        }
                        else
                        {
                            //do do that
                        }
                    }
                }
            }
        }
 
Share this answer
 
Comments
Manoj from Pune 15-Dec-10 8:00am    
Thanks vivek but i want code in VB.NET. Secondly i tried to code on this event, but my question is that on this cell content click how to check that the check box is checked or not?
Manoj from Pune 15-Dec-10 8:07am    
And 1 more thing when u click to check the checkbox and dubug the mentioned event
and u check the state by your code by covert to boolean it is still gives false.
You want this?

VB
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
  If e.RowIndex < 0 Or e.ColumnIndex < 0 Then Exit Sub
  If e.ColumnIndex = 0 Then
    If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value Then
      For Each c As DataGridViewCell In DataGridView1.Rows(e.RowIndex).Cells
        c.Style.BackColor = Color.Black
      Next
    Else
      For Each c As DataGridViewCell In DataGridView1.Rows(e.RowIndex).Cells
        c.Style.BackColor = Color.White
      Next
    End If
  End If
End Sub
 
Share this answer
 
Comments
Manoj from Pune 15-Dec-10 23:44pm    
this is not working,
On check uncheck of checkbox its not working, when a different cell is clicked then this code works.
I want to work on check/uncheck of a checkbox
I got the answer on cell content click i made an object of that cell and coded further like.....
SQL
Dim oCheck As DataGridViewCheckBoxCell
oCheck = datagrid.CurrentRow.Cells("colunmname")



SQL
If oCheck.EditingCellFormattedValue = True Then
 For iCount = 0 To datagrid.CurrentRow.Cells.Count - 1
  datagrid.CurrentRow.Cells(iCount).Style.BackColor = Color.Teal
 Next iCount
Else
 For iCount = 0 To datagrid.CurrentRow.Cells.Count - 1
  datagrid.CurrentRow.Cells(iCount).Style.BackColor = Color.White
  Next iCount
End If
 
Share this answer
 
v3
Comments
Manoj from Pune 16-Dec-10 2:25am    
and its working exactly how i wanted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900