更改DataGrid行的颜色 [英] change color of DataGrid row

查看:169
本文介绍了更改DataGrid行的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据行的一个单元格的内容为 System.Windows.Forms.DataGrid 的整个行着色。

我找不到任何方法,因为没有 .Rows -DataGrid的属性或 .Row -DataGridTextBoxColumn的属性(或其他类似属性)。

到目前为止,在互联网上搜索解决方案也无济于事。

切换到 DataGridView 不幸的是不是一个选择。

I want to color entire rows of a System.Windows.Forms.DataGrid based on the content of one cell of the Row.
I couldn't find any way to do that, since there is no .Rows-Property of the DataGrid or a .Row-Property of the DataGridTextBoxColumn (or any similar).
Searching for solutions in the internet also didn't help me so far.
Switching to DataGridView is unfortunately not an option.

所以问题仍然存在:如何更改DataGrid行的颜色?

So the question remains: How can I change the color of a DataGrid row?

推荐答案

感谢链接(由@Monty提供),我能够解决该问题。

Thanks to this link (provided by @Monty) I was able to solve the problem.

您必须自行格式化每个单元格,因此必须使用 SetCellFormat -Event每列:

You have to format every Cell on its own, so you have to hook up on the SetCellFormat-Event of every Column:

AddHandler column.SetCellFormat, AddressOf FormatGridRow

在EventHandler中,您可以检查例如同一行另一列的单元格值,并相应地更改当前单元格的颜色。当行的每个单元格都以这种方式格式化时,好像整个行都被格式化了。

In the EventHandler you can check for example the cell value of another column of the same row and change the color of the current cell accordingly. When every cell of the row gets formatted this way, it seems like the row as a whole is formatted.

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

这篇关于更改DataGrid行的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆