【问题标题】:change color of DataGrid row更改 DataGrid 行的颜色
【发布时间】:2016-06-04 01:36:35
【问题描述】:

我想根据行中一个单元格的内容为System.Windows.Forms.DataGrid 的整行着色。
我找不到任何方法来做到这一点,因为没有 .Rows-DataGrid 的属性或 .Row-DataGridTextBoxColumn 的属性(或任何类似的)。
到目前为止,在互联网上搜索解决方案也没有帮助我。
不幸的是,切换到DataGridView 是不可行的。

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

【问题讨论】:

标签: vb.net winforms datagrid .net-2.0


【解决方案1】:

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

你必须自己格式化每个单元格,所以你必须连接每个列的SetCellFormat-Event:

AddHandler column.SetCellFormat, AddressOf FormatGridRow

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

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

【讨论】:

  • 很高兴能帮上忙……也祝奥格斯堡足球俱乐部在欧联杯第二回合对阵利物浦的比赛中好运……来自阿森纳球迷……哈哈
猜你喜欢
  • 2017-11-15
  • 2010-12-03
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-16
  • 2016-06-16
  • 2015-12-26
  • 2015-01-11
相关资源
最近更新 更多