2016-08-18 63 views
0

我有一個ReadOnly DataGridView綁定到bindingSource。表單上還有其他控件可以更新DataGridViewCells - 它們是綁定到綁定源的數據綁定。我需要根據DataViewRowState的源代碼更改datagridviewCell樣式 - .Current不會幫助我 - 我需要做的是track是單元格的原始值 - 爲單元格正常着色,如果它被修改爲藍色。我知道我可以得到DataRowViewState,它給了我所得到的版本 - 但它總是返回當前(因爲它是默認值),我確實需要當前 - 我只想知道它是否與原始版本不同這個特定的列。因爲在我編輯bindingsource的時候,我已經有了這行,看起來並不是一個簡單的方法或屬性,說這行是原始的,或者是修改的。我是否需要在表格上使用filterstates來運行select以獲取我的行,並查看是否返回結果,如果是這樣,請更改單元格以執行此操作?使用DataViewRowState將樣式分配給DataGridViewCell

相關代碼: - 我知道它是在VB.net中,但如果你有C#示例,那很好,我不在乎 - .net代碼是代碼,我可以將其轉換。

Private Sub UpdateCellValue(columnName As String, textValue As String) 

    If dgvBayList.SelectedRows.Count > 0 Then 
     Dim crow As DataGridViewRow = dgvBayList.SelectedRows(0) 
     Dim drv As DataRowView = DirectCast(bsBins.Current, DataRowView) 

     If crow.Cells(columnName).Value = textValue Then 
      Exit Sub 
     End If 

     drv.BeginEdit() 
     drv.Row.BeginEdit() 
     drv.Row.SetField(Of String)(columnName, textValue) 
     ' My Problem here is of course it is always original, 
     ' the row states of the Row always show Modified. 
     ' This happens when the binding source gets filled and bound to datagridview. 

     Select Case drv.RowVersion 
      Case DataRowVersion.Original 
       Dim val As String = drv.Row(0).ToString() 
       Exit Select 
      Case DataRowVersion.Proposed 
       Dim val As String = drv.Row(0).ToString() 
       Exit Select 
      Case DataRowVersion.Current 
       Dim val As String = drv.Row(0).ToString() 
       Exit Select 
      Case DataRowVersion.Default 
       Dim val As String = drv.Row(0).ToString() 
       Exit Select 
     End Select 
     crow.Cells(columnName).Value = textValue 
     drv.Row.EndEdit() 
     drv.EndEdit() 
     Dim dt As DataTable = sortingDataSet.bins 

    End If 

End Sub 
+1

看來你需要drv.Row.RowState不DRV .RowVersion – Steve

+0

@Steve我的RowState總是被修改甚至在修改之前;我不知道爲什麼它是這樣的。綁定源綁定到一個dataset.Table,它在表單加載時被填充。因爲它總是聲明從開始修改 - 這並沒有幫助我。我還需要知道用戶是否將它改回原來的 - 我希望它看起來像一個正常的不變的單元格。我想我需要選擇使用rowfilter - 應該有一個更簡單的方法。我希望有一個。 – Ken

+0

如果我查看單元格格式事件中的'RowState',它對我來說工作正常。 – Plutonix

回答

0

看評論,並使用不同的想法進行搜索後,我發現了一個SO答案,所提供的解決方案How can I get a specific version of a dataset row?的一部分。

爲了實現這個解決方案,我需要建立一個調用通用函數的機制,其中T是未知的,直到運行時。在重新審視之後 - 我發現了一些很好的答案; How do I use reflection to call a generic method?

下面是我使用的最終代碼從我的控件更改值來實現對datagridview的更改並將樣式應用於基於原始值或建議值的各個單元格。

Private Sub UpdateCellValue(Of T)(columnName As String, value As T) 

    If dgvBayList.SelectedRows.Count > 0 Then 

     Dim crow As DataGridViewRow = dgvBayList.SelectedRows(0) 

     Dim drv As DataRowView = DirectCast(bsDataSource.Current, DataRowView) 

     If crow.Cells(columnName).Value = value Then 
      Exit Sub 
     End If 

     drv.BeginEdit() 

     drv.Row.BeginEdit() 

     drv.Row.SetField(Of T)(columnName, value) 

     crow.Cells(columnName).Value = value 

     Dim objType As Type = drv.Row(columnName).GetType() 

     Dim method As Reflection.MethodInfo = _ 
       Me.GetType().GetMethod("SetDataGridViewCellStyle", _ 
       Reflection.BindingFlags.Instance Or _ 
       Reflection.BindingFlags.NonPublic Or _ 
       Reflection.BindingFlags.Public) 

     Dim GenericSetDataGridViewCellStyle As Reflection.MethodInfo = _ 
               method.MakeGenericMethod(objType) 

     Dim parms As Object() = {crow.Cells(columnName), drv, columnName} 

     GenericSetDataGridViewCellStyle.Invoke(Me, parms) 

     drv.Row.EndEdit() 
     drv.EndEdit() 
    End If 

End Sub 

Private Sub SetDataGridViewCellStyle(Of T) (_ 
          cell As DataGridViewCell, _ 
          dRowView As DataRowView, _ 
          columnName As String) 

    Dim nothingness As Integer = 0 

    If dRowView.Row.Field(Of T)(columnName, DataRowVersion.Original) Is Nothing Then 
     nothingness += 1 
    End If 

    If dRowView.Row.Field(Of T)(columnName, DataRowVersion.Current) Is Nothing Then 
     nothingness += 1 
    End If 

    If nothingness = 0 Then 

     Dim original As T = dRowView.Row.Field(Of T)(columnName, DataRowVersion.Original) 
     Dim current As T = dRowView.Row.Field(Of T)(columnName, DataRowVersion.Current) 
     Dim proposed As T = dRowView.Row.Field(Of T)(columnName, DataRowVersion.Proposed) 

     If EqualsCompare(Of T)(original, proposed) Then 

      cell.Style.BackColor = Color.FromKnownColor(KnownColor.White) 

     Else 

      cell.Style.BackColor = Color.FromKnownColor(KnownColor.Fuchsia) 

     End If 

    Else 
     If nothingness = 2 Then 

      cell.Style.BackColor = Color.FromKnownColor(KnownColor.White) 
     Else 
      ' cell.Style.BackColor = Color.FromKnownColor(KnownColor.Fuchsia)' 
     End If 

    End If 

    cell.DataGridView.Update() 

End Sub 

Public Shared Function EqualsCompare(Of T)(a As T, b As T) As Boolean 

    Return EqualityComparer(Of T).[Default].Equals(a, b) 

End Function 

我這樣稱呼它

UpdateCellValue(Of Boolean)("kickingIt", True) 

相關鏈接,幫助我

How can I get a specific version of a dataset row?

How do I use reflection to call a generic method?

相關問題