[英]Change the row color in DataGridView based on the quantity of a cell value
我需要在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
請幫我修理一下。 謝謝。
這可能會有所幫助
我從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
我修正了我的錯誤。 剛刪除此行中的“值”:
If drv.Item("Quantity").Value < 5 Then
所以它看起來像
If drv.Item("Quantity") < 5 Then
試試這個(注意:我現在沒有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
只需刪除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
If drv.Item("Quantity").Value < 5 Then
用它來喜歡這個
If Cint(drv.Item("Quantity").Value) < 5 Then
使用CellFormating事件和e參數:
If CInt(e.Value) < 5 Then e.CellStyle.ForeColor = Color.Red
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.