【问题标题】:How Retain Programmatically-Colored Backgrounds of DataGridView on Sorting如何在排序时保留 DataGridView 的编程颜色背景
【发布时间】:2015-02-01 04:44:22
【问题描述】:

在我在 VS2013 中创建的 VB.NET WinForms 项目中,我有这段代码来检测 DataGridView 的单元格内容何时更改:

Private Sub dgvEmployees_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgvEmployees.CellValueChanged
    ' Pass the row and cell indexes to the method so we can change the color of the edited row
    CompareDgvToDataSource("employees", e.RowIndex, e.ColumnIndex)
End Sub

Private Sub CompareDgvToDataSource(ByVal dataSetName As String, ByVal rowIndex As Integer, ByVal columnIndex As Integer)
    ' Takes a dataset and the row and column indexes, checks if the row is different from the DataSet and colors the row appropriately

    EmployeesBindingSource.EndEdit()

    Dim dsChanges As DataSet = EmployeesDataSet.GetChanges()

    If Not dsChanges Is Nothing Then
        For Each dtrow As DataRow In dsChanges.Tables("employees").Rows
            If DirectCast(dtrow, EmployeesDataSet.employeesRow).employeeID.ToString = dgvEmployees.Rows(rowIndex).Cells("employeeID").Value.ToString Then
                For i As Integer = 0 To dsChanges.Tables("employees").Columns.Count - 1

                    If dtrow.RowState.ToString = DataRowState.Added.ToString Then
                        ' TODO: Color entire new row
                    ElseIf dsChanges.Tables(dataSetName).Rows(0).HasVersion(DataRowVersion.Original) Then
                        If Not dtrow(i, DataRowVersion.Current).Equals(dtrow(i, DataRowVersion.Original)) Then
                            Console.WriteLine("Employees ID: " & DirectCast(dtrow, EmployeesDataSet.employeesRow).employeeID)
                            dgvEmployees.Rows(rowIndex).Cells(columnIndex).Style.BackColor = Color.LightPink
                        Else
                            ' TODO: Need to change the BackColor back to what it should be based on its original alternating row color
                        End If
                    End If
                Next
            End If
        Next
    End If
End Sub

问题是,如果用户用任何颜色的单元格对 DGV 进行排序,那么在排序之后,没有一个单元格是着色的。

我需要做什么才能在排序后为正确的单元格保留单元格背景颜色?

最终工作代码

Private Sub CompareDgvToDataSource()

    ' Force ending Edit mode so the last edited value is committed
    EmployeesBindingSource.EndEdit()

    Dim dsChanged As DataSet = EmployeesDataSet.GetChanges(DataRowState.Added Or DataRowState.Modified)

    If Not dsChanged Is Nothing Then
        Dim dtChanged As DataTable = dsChanged.Tables("employees")

        For Each row As DataRow In dtChanged.Rows
            For Each dgvRow As DataGridViewRow In dgvEmployees.Rows
                If dgvRow.Cells("employeeID").Value IsNot Nothing Then
                    If dgvRow.Cells("employeeID").Value.Equals(row.Item("employeeID")) Then
                        ' Found the row in the DGV that matches the current Changed Row
                        For i As Integer = 0 To dtChanged.Columns.Count - 1

                            If Not row(i, DataRowVersion.Current).Equals(row(i, DataRowVersion.Original)) Then
                                ' Found a Cell in the current DGV row that is different from the DataSet
                                Console.WriteLine("Row index: " & dtChanged.Rows.IndexOf(row))
                                dgvEmployees.Rows(dgvRow.Index).Cells(i + 1).Style.BackColor = Color.LightPink
                            Else
                                ' Need to change the BackColor back to what it should be based on its original alternating row color
                            End If
                        Next
                    End If
                End If

            Next
        Next
    End If
End Sub

【问题讨论】:

  • 这可能是一个愚蠢的问题,但您不能只处理“已排序”事件并在那里添加您的程序化着色吗?

标签: vb.net winforms sorting datagridview visual-studio-2013


【解决方案1】:

我为你的问题 +1 了,因为这是一个有趣的挑战 :-)

我不知道在排序后行或单元格的颜色会丢失。好奇的。这就是我要做的。创建一个整数数组的 ViewState 变量(或其他一些将持续存在的对象/变量)。当您为一行着色时,将该行的 ID 添加到您的变量中。

然后,在 DataGridView.OnSorted 事件中,遍历该数组并对每一行重新着色。

此处有关 DataGridView.OnSorted 事件的信息: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.sorted(v=vs.110).aspx

让我知道这是否有意义,或者您是否需要更多帮助。

编辑:

可能有更好的解决方案:

Windows Forms: DataGridView Problem with backgroundcolor after sorting

另一个编辑:

这家伙通过使用未绑定的 DataGridView 找到了一个创造性的解决方案。按照设计,绑定的 DGV 将在您排序时重新绑定,并且所有样式更改都将丢失。但是如果你使用一个未绑定的 DGV,那么所有的样式都会在排序之后保留。

滚动到最底部,看看他是如何解决的。

https://social.msdn.microsoft.com/forums/windows/en-us/f7bde482-cc02-48be-b917-9fdfab73bc18/datagridview-rows-cells-state-not-retaining-after-sorting

有关创建未绑定的 Windows 窗体 DataGridView 控件的更多信息:

http://msdn.microsoft.com/en-us/library/5s3ce6k8(v=vs.90).aspx

【讨论】:

  • 好东西,凯西!我很惊讶我没有遇到您在搜索时找到的那些示例。我会检查这些东西并在这里发布我的结果。
  • 我在您第一个链接的问题的答案中确定了一个建议。我使用 DataBindingComplete 事件来调用我的遍历 DataSet 的方法。我将发布我的最终工作代码。谢谢!
猜你喜欢
  • 2015-01-03
  • 1970-01-01
  • 1970-01-01
  • 2021-04-26
  • 2011-05-29
  • 2016-07-27
  • 2014-08-06
  • 2014-11-06
  • 1970-01-01
相关资源
最近更新 更多