更改 DataGrid 行的颜色
change color of DataGrid row
我想根据行中一个单元格的内容为 System.Windows.Forms.DataGrid
的整行着色。
我找不到任何方法来做到这一点,因为没有 DataGrid 的 .Rows
-属性 或 DataGridTextBoxColumn 的 .Row
-属性 (或任何类似的) .
到目前为止,在互联网上搜索解决方案也没有帮助我。
不幸的是,切换到 DataGridView
不是一个选项。
所以问题仍然存在:如何更改 DataGrid 行的颜色?
感谢this link(由@Monty 提供)我能够解决问题。
您必须自己格式化每个单元格,因此您必须连接每个列的 SetCellFormat
-事件:
AddHandler column.SetCellFormat, AddressOf FormatGridRow
在事件处理程序中,您可以检查同一行另一列的单元格值,并相应地更改当前单元格的颜色。当行中的每个单元格都以这种方式格式化时,整个行似乎都被格式化了。
Private Sub FormatGridRow(ByVal sender As Object, ByVal e As DataGridFormatCellEventArgs)
Dim Cell As DataGridColumnStyle = sender
Dim ColumnStyles As GridColumnStylesCollection = Cell.DataGridTableStyle.GridColumnStyles
Dim columnIndex As Integer = ColumnStyles.IndexOf(ColumnStyles.Item("ColumnName"))
If Cell.DataGridTableTableStyle.DataGrid(e.Row, columnIndex).ToString() = "SomeValue" Then
e.BackBrush = New SolidBrush(Color.Red) 'this does only change the Cell`s background
End If
End Sub
我想根据行中一个单元格的内容为 System.Windows.Forms.DataGrid
的整行着色。
我找不到任何方法来做到这一点,因为没有 DataGrid 的 .Rows
-属性 或 DataGridTextBoxColumn 的 .Row
-属性 (或任何类似的) .
到目前为止,在互联网上搜索解决方案也没有帮助我。
不幸的是,切换到 DataGridView
不是一个选项。
所以问题仍然存在:如何更改 DataGrid 行的颜色?
感谢this link(由@Monty 提供)我能够解决问题。
您必须自己格式化每个单元格,因此您必须连接每个列的 SetCellFormat
-事件:
AddHandler column.SetCellFormat, AddressOf FormatGridRow
在事件处理程序中,您可以检查同一行另一列的单元格值,并相应地更改当前单元格的颜色。当行中的每个单元格都以这种方式格式化时,整个行似乎都被格式化了。
Private Sub FormatGridRow(ByVal sender As Object, ByVal e As DataGridFormatCellEventArgs)
Dim Cell As DataGridColumnStyle = sender
Dim ColumnStyles As GridColumnStylesCollection = Cell.DataGridTableStyle.GridColumnStyles
Dim columnIndex As Integer = ColumnStyles.IndexOf(ColumnStyles.Item("ColumnName"))
If Cell.DataGridTableTableStyle.DataGrid(e.Row, columnIndex).ToString() = "SomeValue" Then
e.BackBrush = New SolidBrush(Color.Red) 'this does only change the Cell`s background
End If
End Sub