如果单元格包含某个值,我想知道是否有办法将单个数据网格视图单元格的背景颜色更改为红色。例如:
If (columnindex = 1) Then
Dim cellData = DataGridView1.Rows(rowindex).Cells(columnindex).Value
If cellData Is Nothing OrElse IsDBNull(cellData) OrElse cellData.ToString = String.Empty Then
'Do nothing because this is allowed
'Now I want to set the default backcolor for the datagridview to white
DataGridView1.Rows(rowindex).Cells(columnindex).DefaultCellStyle.BackColor = Color.White
ElseIf cellData < 0 Or cellData > 1 Then
MessageBox.Show("Value Must be between 0 and 1")
DataGridView1.Rows(rowindex).Cells(columnindex).Value = 0
'This is where I'm hoping to make only the cells that values are not between 1 or zero have a backcolor of red
DataGridView1.Rows(rowindex).Cells(columnindex).DefaultCellStyle.BackColor = Color.Red
Exit Sub
End If
End If
按照目前的情况,如果一个或多个单元格包含无效数据,我的代码将使datagridview的整个第一列变为红色。我希望只有包含无效数据的单元格是红色的。如果有人能弄清楚这一点,我将不胜感激!:)
像这样访问单元格,而不是
DataGridView1.Item(columnindex, rowindex).Style.BackColor = Color