我有一个像这样的 DataGridView:
我像这样绑定(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/