我需要在datagridview中更改行的颜色,但我的代码对我不起作用。 我总是收到一个错误,上面写着“名为Quantity的列:无法找到。参数名称:columnName”
这是我的代码:
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
请帮我修理一下。谢谢。
答案 0 :(得分:12)
这可能会有所帮助
我将其转换为C#(&#39;来自:http://www.dotnetpools.com/Article/ArticleDetiail/?articleId=74)
Private Sub dgv_EmployeeTraining_RowPostPaint(sender As Object, e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs)
Handles dgv_EmployeeTraining.RowPostPaint
If e.RowIndex < Me.dgv_EmployeeTraining.RowCount - 1 Then
Dim dgvRow As DataGridViewRow = Me.dgv_EmployeeTraining.Rows(e.RowIndex)
'<== This is the header Name
'If CInt(dgvRow.Cells("EmployeeStatus_Training_e26").Value) <> 2 Then
'<== But this is the name assigned to it in the properties of the control
If CInt(dgvRow.Cells("DataGridViewTextBoxColumn15").Value.ToString) <> 2 Then
dgvRow.DefaultCellStyle.BackColor = Color.FromArgb(236, 236, 255)
Else
dgvRow.DefaultCellStyle.BackColor = Color.LightPink
End If
End If
End Sub
答案 1 :(得分:3)
我修正了错误。刚删除此行中的“值”:
If drv.Item("Quantity").Value < 5 Then
所以它看起来像
If drv.Item("Quantity") < 5 Then
答案 2 :(得分:1)
试试这个(注意:我现在没有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
答案 3 :(得分:1)
只需移除:
中的Quantity
即可。确保您的属性与代码中包含的参数相同,如下所示:
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
答案 4 :(得分:0)
If drv.Item("Quantity").Value < 5 Then
使用它来喜欢这个
If Cint(drv.Item("Quantity").Value) < 5 Then
答案 5 :(得分:0)
使用 CellFormating事件和 e 参数:
If CInt(e.Value) < 5 Then e.CellStyle.ForeColor = Color.Red
答案 6 :(得分:0)
DATE_FORMAT(STR_TO_DATE(monthNumber, '%m'), '%b')