在我在 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.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
另一次编辑:
这个人通过使用未绑定(bind)的 DataGridView 找到了一个创造性的解决方案。根据设计,绑定(bind)的 DGV 将在您排序时重新绑定(bind),并且所有样式更改都会丢失。但如果您使用未绑定(bind)的 DGV,则排序后所有样式都会保留。
滚动到最底部,看看他是如何解决这个问题的。
有关创建未绑定(bind) Windows 窗体 DataGridView 控件的更多信息:
http://msdn.microsoft.com/en-us/library/5s3ce6k8(v=vs.90).aspx
关于vb.net - 如何在排序时保留 DataGridView 的编程彩色背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27274795/