vb.net 根据单元格值的数量更改DataGridView中的行颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13378538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the row color in DataGridView based on the quantity of a cell value
提问by Rara Arar
I need to change the color of a row in datagridview but my code is not working for me. I always get a error that says "Column named Quantity: cannot be found. Parameter name: columnName"
我需要更改 datagridview 中一行的颜色,但我的代码对我不起作用。我总是收到一条错误消息,指出“名为数量的列:无法找到。参数名称:列名”
Here is my code:
这是我的代码:
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
Please help me fix it. Thank you.
请帮我修复它。谢谢你。
采纳答案by Rara Arar
I fixed my error. just removed "Value" from this line:
我修正了我的错误。刚刚从这一行中删除了“价值”:
If drv.Item("Quantity").Value < 5 Then
So it will look like
所以它看起来像
If drv.Item("Quantity") < 5 Then
If drv.Item("Quantity") < 5 Then
回答by glenn garson
This might be helpful
这可能会有所帮助
- Use the "RowPostPaint" event
- The name of the column is NOT the "Header" of the column. You have to go to the properties for the DataGridView => then select the column => then look for the "Name" property
- 使用“RowPostPaint”事件
- 列的名称不是列的“标题”。您必须转到 DataGridView 的属性 => 然后选择列 => 然后查找“名称”属性
I converted this from C# ('From: http://www.dotnetpools.com/Article/ArticleDetiail/?articleId=74)
我从 C# 转换了这个('来自: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
回答by Jonel Taruc
Just remove the :
in your Quantity
. Make sure that your attribute is the same with the parameter you include in the code, like this:
只需删除:
您的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
回答by Nianios
Try this (Note: I don't have right now Visual Studio ,so code is copy paste from my archive(I haven't test it) :
试试这个(注意:我现在没有 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
回答by Thanush Wijesinghe
If drv.Item("Quantity").Value < 5 Then
use this to like this
用这个来喜欢这个
If Cint(drv.Item("Quantity").Value) < 5 Then
回答by Elian
Using the CellFormating eventand the eargument:
使用CellFormating 事件和e参数:
If CInt(e.Value) < 5 Then e.CellStyle.ForeColor = Color.Red
If CInt(e.Value) < 5 Then e.CellStyle.ForeColor = Color.Red
回答by user6533074
Dim dgv As DataGridView = Me.TblCalendarDataGridView
For i As Integer = 0 To dgv.Rows.Count - 1
For ColNo As Integer = 4 To 7
If Not dgv.Rows(i).Cells(ColNo).Value Is DBNull.Value Then
dgv.Rows(i).Cells(ColNo).Style.BackColor = vbcolor.blue
End If
Next
Next