2014-12-03 59 views
2

在VB.NET WinForms項目,我創造了VS2013我有這樣的代碼來檢測的DataGridView的編程方式,有色背景:這個問題如何當一個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 
+0

這可能是一個愚蠢的問題,但你不能只處理「排序」事件,並添加自己的程序在那裏着色? – Keith 2014-12-03 16:23:41

回答

2

我您的問題+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

ANOTHER編輯:

這傢伙找到一個創造性的解決方案通過使用綁定的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

+0

好東西,凱西!我很驚訝我沒有遇到你在搜索時發現的那些例子。我會通過這些東西,並在這裏發佈我的結果。 – marky 2014-12-03 15:44:56

+1

我在第一個鏈接的回答中解決了一個建議。我使用DataBindingComplete事件來調用我的迭代DataSet的方法。我會發布我的最終工作代碼。謝謝! – marky 2014-12-03 17:01:59

相關問題