2012-11-14 169 views
7

我需要更改datagridview中行的颜色,但我的代码不适合我。 我总是写着一个错误“列命名数量:无法找到参数名称:列名”根据单元格的数量更改DataGridView中的行颜色

这里是我的代码:

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

使用我的以下回答链接进行日期范围比较 http://stackoverflow.com/a/29486288/3583859 –

回答

2

我固定我的错误只是删除。 “值” 从这一行:

If drv.Item("Quantity").Value < 5 Then 

所以它看起来像

If drv.Item("Quantity") < 5 Then

+0

对不起,我在旅途中。所以我现在无法帮助你。我希望你的修复,解决了你的问题 – Nianios

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 
+0

非常感谢您的回答,但我有几个关于您的答案的问题:) 它显示此错误: 对象引用未设置为对象的实例。 和“产品”是指?它是数据源的名称吗? –

+0

ds.Tables(“Products”)是填充datagridview的表格。 所以把你的餐桌上的名字 – Nianios

+0

Thankyou HaBouF。我得到的错误怎么样?我该如何解决这个问题? –

11

这可能是有益的

  1. 使用“RowPostPaint”事件
  2. 列的名称不是列的“头”。你必须去为DataGridView的属性=>然后选择栏=>然后寻找

我转换这从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 
0
If drv.Item("Quantity").Value < 5 Then 

使用这种喜欢这个

If Cint(drv.Item("Quantity").Value) < 5 Then 
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 
0

使用CellFormating事件Ë说法:

If CInt(e.Value) < 5 Then e.CellStyle.ForeColor = Color.Red

0
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 
+0

请添加你的解答 – STF

相关问题