views:

370

answers:

1

I have a C# Windows application that uses a DataGridView with three columns. The first is a textbox that requires no validation. The second and third columns are both checkboxes. I need help with determining if the boxes are checked or not. If they are then I would like to set the background color of the checkbox cell to red. I am using the DataGridView1_CellContentClick event for this. Any help or suggestions would be appreciated.

A: 

Hi, try this

void DataGridView1_CellValueChanged(object sender System.Windows.Forms.DataGridViewCellEventArgs e)
{
  if( (bool)DataGridView1.Rows[e.RowIndex].Cells[ e.ColumnIndex].Value )
             DataGridView1.Rows[e.RowIndex].Cells[ e.ColumnIndex].DefaultCellStyle.BackColor  = Color.Red;
}
IordanTanev
I need the code to determine if the box is checked or unchecked.
DaBomb
if the clicked cell is DataGridViewCheckBoxCell DataGridView1.Rows[e.RowIndex].Cells[ e.ColumnIndex].Value will return true if it is checked and false it it is unchecked
IordanTanev
I get a build error on "DefaultCellStyle.BackColor". It states "'System.Windows.Forms.DataGridViewCell' does not contain a definition for 'DefaultCellStyle' ...."I changed it to "DataGridView.BackgroundColor".My code now compiles but I get an error on "DataGridView.BackgroundColor". It states "Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index". This event is firing when the application runs before I attempt to enter a new row and before the form is even displayed.
DaBomb
I forgot to mention that this event fires even when you add rows to gridview by code. So if this is the case just add rows to gridview and then attach the event to the gridview
IordanTanev