在我在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
我+1你的问题,因为这是一个有趣的挑战:-(
我不知道行或单元格的颜色会在排序后丢失。 好奇。 这就是我要做的。 创建一个 ViewState 变量(或将保留的其他一些对象/变量(,它是一个整数数组。 为行着色时,将该行的 ID 添加到变量中。
然后,在 DataGridView.OnSort 事件上,运行该数组并重新着色每一行。
有关 DataGridView.OnSort 事件的信息,请参阅此处:http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.sorted(v=vs.110(.aspx
莱姆知道这是否有意义,或者你是否需要更多帮助。
编辑:
可能有更好的解决方案:
Windows 窗体:排序后背景颜色的数据网格视图问题
另一个编辑:
这家伙通过使用未绑定的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