Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net DataGridView:为值为0或1的所有单个单元格设置背景色样式_Vb.net_Datagridview - Fatal编程技术网

Vb.net DataGridView:为值为0或1的所有单个单元格设置背景色样式

Vb.net DataGridView:为值为0或1的所有单个单元格设置背景色样式,vb.net,datagridview,Vb.net,Datagridview,在MyBase.Load上,我连接到SQL server,获取所需信息,然后填充dataGridView,使其显示为矩阵 索引0处的列都是我的部件名,行标题都是我的接收器型号 矩阵为1或0 我要对值为0和0的所有单元格执行:cell.Style.BackColor=Color.Red 我要对值为1的所有单元格执行:cell.Style.BackColor=Color.LightGreen 到目前为止,我正在尝试: `Private Sub dgCorpResults_CurrentCellCha

在MyBase.Load上,我连接到SQL server,获取所需信息,然后填充dataGridView,使其显示为矩阵

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

矩阵为1或0

我要对值为0和0的所有单元格执行:cell.Style.BackColor=Color.Red 我要对值为1的所有单元格执行:cell.Style.BackColor=Color.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)

当我使用:dgvMatrix.Rows(e.RowIndex).Cells(0.ToString()=0时,我得到以下错误:“从字符串”DataGridViewTextBoxCell{Column“转换为”类型“Double”无效。”以及“从字符串”DataGridViewTextBoxCell{Column“转换为”DataGridViewTextBoxCell{Column”“输入‘Boolean’无效。”当我使用If-dgvMatrix.Rows(e.RowIndex).Cells(0).ToString()时,很抱歉,请将.ToString()更改为.Text