[英]How to change DataGridView cell color based on value of Combobox?
[英]DataGridView: How to change cell color depending on data source value
如果我的数据集有以下记录
Col1 Col2 Col3
A D H
B E I
C F J
我想突出显示值为 C、E、F 的单元格。 突出显示应在将数据源分配给 DataGridView 期间发生。 尝试了 CellValueChanged 事件,但无法使其实现我想要做的事情。
我希望不要 go 迭代每个单元格并验证值。
可行吗?
蒂亚!
我没有过多地使用CellFormatting
或CellPainting
,但我认为在这种情况下, CellFormatting
可能是更好的选择。 这个例子对我有用:
Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
Dim cell = DataGridView1(e.ColumnIndex, e.RowIndex)
Select Case TryCast(cell.Value, String)
Case "C", "E", "F"
e.CellStyle.BackColor = Color.Red
End Select
End Sub
每次单元格需要格式化时,每个单元格都会引发CellFormatting
。 这意味着,与CellValueChanged
不同,它将在最初创建单元格时引发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.