DataGridView:为所有值为0或1的单元格设置Style backColor



加载,我连接到一个SQL服务器,得到我需要的信息,然后填充一个dataGridView,使其显示为一个矩阵。

索引0处的列是我所有的部件名称,行标题是我所有的接收器型号。

矩阵为1或0。

我想执行:cell.Style.BackColor = Color。红色表示值为0和的所有单元格我想执行:cell。style。backcolor = Color。值为1

的所有单元格的LightGreen

我正在尝试这个到目前为止:

`Private Sub dgCorpResults_CurrentCellChanged(ByVal sender As Object, ByVal e As    
 System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvMatrix.CellFormatting

Try
For Each cell As DataGridViewCell In dgvMatrix.Rows(e.RowIndex).Cells
cell.Style.BackColor = Color.Red
Next  
Catch ex As Exception  
End Try  

子结束">

这样我就可以开始构思如何只画我需要画的细胞。

专家能帮我吗?

看起来您需要一个简单的If Then语句。假设您绝对需要Try语句,那么您只需要在For Each语句

中包含如下内容
If value=0 Then
cell.Style.BackColor = Color.Red
Else
cell.Style.BackColor = Color.LightGreen
End If

再一次,这是假设你绝对需要剩下的,但对我来说,它看起来可以简化。

编辑:当然,你还需要能够确定如何获得"值"。在这种情况下,似乎要么value=cell.ToString()或dgvMatrix.Rows(e.RowIndex).Cell(0).Text

最新更新