1
votes

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.

3
I forgot to mention. The data is entered by the user, not from a DataSource.DaBomb

3 Answers

0
votes

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;
}
0
votes

Try the following:

    bool test=false;
         test=Convert.Toboolean(DataGridView1.Rows[0].cells[0].Value);
   // if test=true then
   // its checked and if no then its unchecked. 
-1
votes
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    Try
        If e.ColumnIndex = 8 Then
            Label1.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value
            FrmVisits.Show()

        ElseIf e.ColumnIndex = 9 Then
            Label1.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value
            FrmPEdit.Show()
        Else
            Exit Sub
        End If
    Catch ex As Exception
        Exit Sub
    End Try




End Sub