0

如果单元格包含某个值,我正在尝试找出是否有办法将单个 datagridviewcell 的背景颜色更改为红色。例如:

 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 的整个第一列变为红色。我希望只有具有无效数据的单元格是红色的。如果有人能弄清楚这一点,我将不胜感激!:)

4

1 回答 1

2

像这样访问单元格

DataGridView1.Item(columnindex, rowindex).Style.BackColor = Color
于 2013-07-24T18:53:08.540 回答