vb.net - 根据值更改数据 GridView 行颜色

标签 vb.net

我有一个像这样的 DataGridView: enter image description here

我像这样绑定(bind) DataGridView:

Dim cmd As New SqlCommand("DashBordFetch", con.connect)  
cmd.CommandType = CommandType.StoredProcedure   
cmd.Parameters.Add("@locid", SqlDbType.Int).Value = locid
da.SelectCommand = cmd
da.Fill(ds)
DGVDashBoard.DataSource = ds.Tables(0)

我希望我的 DataGridView 在值为 1 时具有红色行颜色。我该怎么做?我正在 VB.NET 中工作。

我已经尝试过提供的答案之一中的代码,它看起来像这样:

最佳答案

试试这个

 Private Sub DGVDashBoard_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DGVDashBoard.CellFormatting
            Dim drv As DataRowView
            If e.RowIndex >= 0 Then
                ' Add Your condition here ignore following
                If e.RowIndex <= ds.Tables(0).Rows.Count - 1 Then
                    drv = ds.Tables(0).DefaultView.Item(e.RowIndex)
                    Dim c As Color
                    If drv.Item("Value").ToString = "1" Then
                        c = Color.Red                        
                    End If
                    e.CellStyle.BackColor = c
                End If
            End If
        End Sub

如果这不起作用,请告诉我。

关于vb.net - 根据值更改数据 GridView 行颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17858002/

相关文章:

vb.net - 使抽绳部分加粗

vb.net - list<Of T> 上的多种排序

c# - .NET 用户设置 - DEBUG 和 RELEASE 模式不同

sql - 使用sql参数插入值

vb.net - 什么都不转换到值类型 : different behavior depending on the number of indirection

ASP.NET VB 嵌套 ListView

c# - VB转C#帮助(?运算符)

c# - 检查字符串是否包含列表(字符串)中的元素

.net - 搜索字典键中的某些部分并更新其值

arrays - 从字符数组转换时的字符串长度