如何在 Datagridview vb.net 中为特定单元格着色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29646766/
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
How to color specific cell in Datagridview vb.net?
提问by Daniel Serban
I am trying to color specific cell in data gridvie that have the content "50" and i am using the following code: but is not working. can u please help me color specific cells in datagridview where the data inside cell is "50" :??
我正在尝试为内容为“50”的数据 gridvie 中的特定单元格着色,我正在使用以下代码:但不起作用。你能帮我在datagridview中为特定单元格着色,其中单元格内的数据为“50”:??
Imports System.Data.SqlClient
Public Class Form1
Private ds As New DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strConn As String
strConn = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\smdData.mdf;Integrated Security=True;User Instance=True"
Dim conn As New SqlConnection(strConn)
Dim da As New SqlDataAdapter("select Model from smdTable", conn)
da.Fill(ds, "Model")
DataGridView1.DataSource = ds.Tables("Model")
End Sub
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("smdTable").Rows.Count - 1 Then
drv = ds.Tables("smdTable").DefaultView.Item(e.RowIndex)
Dim c As Color
If drv.Item("Model").ToString = "50" Then
c = Color.LightBlue
Else
c = Color.Pink
End If
Me.DataGridView1.Rows(e.RowIndex).Cells("Model").Style.BackColor = c
End If
End If
End Sub
End Class
回答by Daniel Serban
meanwhile i;ve searched the internet and find an different answer for my question i use the following 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("ModelDataGridViewTextBoxColumn").Value = "50" Then
Me.DataGridView1.Rows(i).Cells("ModelDataGridViewTextBoxColumn").Style.BackColor = Color.Red
End If
Next
End Sub
I get the error : "An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
我收到错误消息:“Microsoft.VisualBasic.dll 中发生了类型为‘System.InvalidCastException’的未处理异常
Additional information: Operator '=' is not defined for type 'DBNull' and string "50"." if i click an cell in datagridview!
附加信息:运算符 '=' 未为类型“DBNull”和字符串“50”定义。如果我单击 datagridview 中的单元格!
回答by matzone
You need Rowprepaint ..
你需要 Rowprepaint ..
Private Sub DataGridView1_RowPrePaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
Dim dgv As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
If dgv.Cells("Model").Value = "50" Then
DataGridView1.Rows(e.RowIndex).DefaultCellStyle.ForeColor = Color.Red
Else
DataGridView1.Rows(e.RowIndex).DefaultCellStyle.ForeColor = Color.Black
End If
End Sub

