如何在Datagridview vb.net中为特定单元格着色?

时间:2015-04-15 09:38:48

标签: vb.net datagridview

我正在尝试为数据gridvie中的特定单元格着色,其内容为“50”并且我使用以下代码:但是无效。 请问帮我在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

3 个答案:

答案 0 :(得分:1)

同时我搜索了互联网并找到了我的问题的不同答案我使用以下代码:

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

我收到错误:“Microsoft.VisualBasic.dll中发生了'System.InvalidCastException'类型的未处理异常

附加信息:未对“DBNull”类型和字符串“50”定义运算符“=”。“如果我单击datagridview中的单元格!

答案 1 :(得分:0)

你需要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

答案 2 :(得分:0)

只需输入要检查的单元格值是否为50。我在报价单上输入INPUT CELL NO。在这里

 ` Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
        DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True

        For Each rw As DataGridViewRow In dgvStocks.Rows
            If rw.Cells(INPUT CELL NO. HERE!).Value = 50 Then
                rw.DefaultCellStyle.BackColor = Color.Red
                rw.DefaultCellStyle.ForeColor = Color.White
            Else
                rw.DefaultCellStyle.BackColor = Color.DarkGreen
                rw.DefaultCellStyle.ForeColor = Color.Yellow
            End If
        Next
    End Sub `