使用 columns 键和为每个更改超网格中的单元格颜色

Posted

技术标签:

【中文标题】使用 columns 键和为每个更改超网格中的单元格颜色【英文标题】:Changing the cell color in an ultragrid with the columns key and for each 【发布时间】:2021-12-17 16:16:00 【问题描述】:

我的目标是为我已经着色的完全相同的单元格着色,但之前只有一列。我试着用索引来做,但没有成功。我得到一个提示,我应该用 Key 属性来做,但我不知道怎么做。这是我尝试过的:

For Each column As UltraGridColumn In ugResult.DisplayLayout.Bands(0).Columns

                If column.Key = "K_Art" Or column.Key = "UANR" Or column.Key = "Ueberbegriff" Or column.Key = "Benennung" Or column.Key = "Anzahl" Or column.Key = "Einheit" Or column.Key = "Einzelkosten" Or column.Key = "Sumcode" Or column.Key = "Status" Then
                    Exit For
                Else
                    If e.Row.Cells(column.Key).Value IsNot Nothing Then

                        e.Row.Cells(column.Key).Appearance.BackColor = Color.Yellow
                        e.Row.Cells(column.Index - 1).Appearance.BackColor = Color.Yellow
                    End If

                End If
            Next

感谢您对 c# 和 vb.net 的任何帮助。谢谢

【问题讨论】:

【参考方案1】:

这是我的解决方案:

For Each column As UltraGridColumn In ugResult.DisplayLayout.Bands(0).Columns
                Select Case column.Key
                    Case "K_ArtCompare"
                        If e.Row.Cells(column.Key).Value IsNot Nothing Then
                            e.Row.Cells(column.Key).Appearance.BackColor = Color.Yellow
                            e.Row.Cells("K_Art").Appearance.BackColor = Color.Yellow
                        Else
                            e.Row.Cells(column.Key).Value = e.Row.Cells("K_Art").Value
                        End If
                    Case "UANR_Compare"
                        If e.Row.Cells(column.Key).Value IsNot Nothing Then
                            e.Row.Cells(column.Key).Appearance.BackColor = Color.Yellow
                            e.Row.Cells("UANR").Appearance.BackColor = Color.Yellow
                        Else
                            e.Row.Cells(column.Key).Value = e.Row.Cells("UANR").Value
                        End If
                    Case "UeberbegriffCompare"
                        If e.Row.Cells(column.Key).Value IsNot Nothing Then
                            e.Row.Cells(column.Key).Appearance.BackColor = Color.Yellow
                            e.Row.Cells("Ueberbegriff").Appearance.BackColor = Color.Yellow
                        Else
                            e.Row.Cells(column.Key).Value = e.Row.Cells("Ueberbegriff").Value
                        End If
                    Case "BenennungCompare"
                        If e.Row.Cells(column.Key).Value IsNot Nothing Then
                            e.Row.Cells(column.Key).Appearance.BackColor = Color.Yellow
                            e.Row.Cells("Benennung").Appearance.BackColor = Color.Yellow
                        Else
                            e.Row.Cells(column.Key).Value = e.Row.Cells("Benennung").Value
                        End If
                    Case "AnzahlCompare"
                        If e.Row.Cells(column.Key).Value <> -1 Then
                            e.Row.Cells(column.Key).Appearance.BackColor = Color.Yellow
                            e.Row.Cells("Anzahl").Appearance.BackColor = Color.Yellow
                        Else
                            e.Row.Cells(column.Key).Value = e.Row.Cells("Anzahl").Value
                        End If
                    Case "EinheitCompare"
                        If e.Row.Cells(column.Key).Value IsNot Nothing Then
                            e.Row.Cells(column.Key).Appearance.BackColor = Color.Yellow
                            e.Row.Cells("Einheit").Appearance.BackColor = Color.Yellow
                        End If
                    Case "EinzelkostenCompare"
                        If e.Row.Cells(column.Key).Value <> -1 Then
                            e.Row.Cells(column.Key).Appearance.BackColor = Color.Yellow
                            e.Row.Cells("Einzelkosten").Appearance.BackColor = Color.Yellow
                        Else
                            e.Row.Cells(column.Key).Value = e.Row.Cells("Einzelkosten").Value
                        End If
                    Case "SummencodeCompare"
                        If e.Row.Cells(column.Key).Value IsNot Nothing Then
                            e.Row.Cells(column.Key).Appearance.BackColor = Color.Yellow
                            e.Row.Cells("Sumcode").Appearance.BackColor = Color.Yellow
                        Else
                            e.Row.Cells(column.Key).Value = e.Row.Cells("Sumcode").Value
                        End If
                End Select

            Next
    End Select

【讨论】:

【参考方案2】:

我认为这会奏效:

For Each column As UltraGridColumn In ugResult.DisplayLayout.Bands(0).Columns
     If column.Key = "K_Art" Or column.Key = "UANR" Or column.Key = "Ueberbegriff" Or column.Key = "Benennung" Or column.Key = "Anzahl" Or column.Key = "Einheit" Or column.Key = "Einzelkosten" Or column.Key = "Sumcode" Or column.Key = "Status" Then
           Exit For
     Else
           If e.Row.Cells(column.Key).Value IsNot Nothing Then
                e.Row.Cells(column.Index).Appearance.BackColor = Color.Yellow
                e.Row.Cells(column.Index - 1).Appearance.BackColor = Color.Yellow
         End If
     End If
Next

【讨论】:

不,它没有。它将整个列着色为两个索引。【参考方案3】:

您应该像这样将单元格的值与DbNull.Value 进行比较:

For Each column As UltraGridColumn In ugResult.DisplayLayout.Bands(0).Columns
    If column.Key = "K_Art" Or column.Key = "UANR" Or column.Key = "Ueberbegriff" Or column.Key = "Benennung" Or column.Key = "Anzahl" Or column.Key = "Einheit" Or column.Key = "Einzelkosten" Or column.Key = "Sumcode" Or column.Key = "Status" Then
        Exit For
    Else
        If Not DbNull.Value.Equals(e.Row.Cells(column.Key).Value) Then
            e.Row.Cells(column.Key).Appearance.BackColor = Color.Yellow
            e.Row.Cells(column.Index - 1).Appearance.BackColor = Color.Yellow
        End If
    End If
Next

【讨论】:

为什么? if 语句工作正常,问题是 (column.Index - 1) 不起作用 回答为什么 (column.Index - 1) 我们需要知道您的列的结构。顺便说一句,您是否使用 (column.Index) 而不是 (column.Key) 对此进行了测试? 是的,我做到了。我找到了解决方案,我会在这里发布。

以上是关于使用 columns 键和为每个更改超网格中的单元格颜色的主要内容,如果未能解决你的问题,请参考以下文章

WPF使用转换器更改datagrid单元格背景颜色

如何更改数据网格中的单元格颜色

如何在 CSS Grid 中突出显示行中的所有单元格(显示:网格)

使用缩放视图填充网格单元

是否可以在datagridview中合并单元格

图像不占用DataGridCell中的整个空间