Vb.net 根据单元格值的数量更改DataGridView中的行颜色
我需要在datagridview中更改行的颜色,但我的代码不适合我。 我总是遇到一个错误,即找不到名为Quantity:的列。参数名称:columnName 这是我的密码:Vb.net 根据单元格值的数量更改DataGridView中的行颜色,vb.net,winforms,datagridview,Vb.net,Winforms,Datagridview,我需要在datagridview中更改行的颜色,但我的代码不适合我。 我总是遇到一个错误,即找不到名为Quantity:的列。参数名称:columnName 这是我的密码: Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting For i As
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
If Me.DataGridView1.Rows(i).Cells("Quantity:").Value < 5 Then
Me.DataGridView1.Rows(i).Cells("Quantity:").Style.ForeColor = Color.Red
End If
Next
End Sub
请帮我修一下。谢谢。请尝试以下注意事项:我现在没有Visual Studio,所以代码是从我的存档中复制粘贴的,我还没有测试它:
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
Dim drv As DataRowView
If e.RowIndex >= 0 Then
If e.RowIndex <= ds.Tables("Products").Rows.Count - 1 Then
drv = ds.Tables("Products").DefaultView.Item(e.RowIndex)
Dim c As Color
If drv.Item("Quantity").Value < 5 Then
c = Color.LightBlue
Else
c = Color.Pink
End If
e.CellStyle.BackColor = c
End If
End If
End Sub
我修正了我的错误。刚刚从此行中删除了值:
If drv.Item("Quantity").Value < 5 Then
所以看起来像
如果drv.ItemQuantity<5,则这可能会有所帮助
使用RowPostPaint事件
列的名称不是列的标题。您必须转到DataGridView=>的属性,然后选择column=>然后查找Name属性
我将其从C'转换为:
用这个来喜欢这个
If Cint(drv.Item("Quantity").Value) < 5 Then
只需去掉你数量中的:即可。确保属性与代码中包含的参数相同,如下所示:
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
If Me.DataGridView1.Rows(i).Cells("Quantity").Value < 5 Then
Me.DataGridView1.Rows(i).Cells("Quantity").Style.ForeColor = Color.Red
End If
Next
End Sub
使用CellFormating事件和e参数:
如果CInte.Value<5,则e.CellStyle.ForeColor=Color.Red将dgv调暗为DataGridView=Me.TblCalendarDataGridView
对于i作为整数=0到dgv.Rows.Count-1
对于ColNo,整数=4到7
如果不是dgv.Rowsi.CellsColNo.Value是DBNull.Value,则
dgv.Rowsi.CellsColNo.Style.BackColor=vbcolor.blue
如果结束
下一个
下一个
非常感谢您的回答,但我对您的回答有几个问题:它显示了以下错误:对象引用未设置为对象的实例。产品指的是什么?它是数据源的名称吗?ds.TablesProducts是填充datagridview的表。所以把你的桌子的名字写在这里谢谢你,哈布夫。我得到的错误是什么?我该怎么办?对不起,我在旅行。所以我现在帮不了你更多。我希望您的修复,解决了您的问题使用我下面回答的链接进行日期跨度比较,请添加对您答案的解释
If Cint(drv.Item("Quantity").Value) < 5 Then
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
If Me.DataGridView1.Rows(i).Cells("Quantity").Value < 5 Then
Me.DataGridView1.Rows(i).Cells("Quantity").Style.ForeColor = Color.Red
End If
Next
End Sub