在vb.net怎么根据查询结果,改变DataGridView中符合条件记录的颜色
在vb.net如何根据查询结果,改变DataGridView中符合条件记录的颜色
通过查询在DataGridView1中得到以下记录:
ID 姓名 性别 年龄 工资 备注
1 张三 男 20 5000 是
2 李四 女 26 4000 否
3 王五 男 24 3000 否
~~~~~~
如何将备注为“是”的记录背景设置为红色
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------
Private Sub DataGridView1_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
If (e.RowIndex >= 0 And e.ColumnIndex >= 0 And e.RowIndex < DataGridView1.Rows.Count - 1) Then
Dim wj As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
If wj.Cells("备注").Value.ToString() = "是" Then
wj.DefaultCellStyle.BackColor = Color.Yellow
End If
End If
End Sub
通过查询在DataGridView1中得到以下记录:
ID 姓名 性别 年龄 工资 备注
1 张三 男 20 5000 是
2 李四 女 26 4000 否
3 王五 男 24 3000 否
~~~~~~
如何将备注为“是”的记录背景设置为红色
------解决方案--------------------
For i=1 To DataGridView1.Rows.Count-1
If DataGridView1.Rows(i - 1).Cells(5).Value = "是" Then
DataGridView1.Rows(i - 1).Cells(5).Style.BackColor = Color.Red
End If
Next
------解决方案--------------------
For i=1 To DataGridView1.Rows.Count-1
If DataGridView1.Rows(i - 1).Cells(5).Value = "是" Then
DataGridView1.Rows(i - 1).Cells(5).Style.BackColor = Color.Red
else
DataGridView1.Rows(i - 1).Cells(5).Style.BackColor = Color.white
End If
Next
------解决方案--------------------
Private Sub DataGridView1_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
If (e.RowIndex >= 0 And e.ColumnIndex >= 0 And e.RowIndex < DataGridView1.Rows.Count - 1) Then
Dim wj As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
If wj.Cells("备注").Value.ToString() = "是" Then
wj.DefaultCellStyle.BackColor = Color.Yellow
End If
End If
End Sub