使用DataGridViewCell给DataGridViewCell分配样式
本文关键字:DataGridViewCell 样式 分配 使用 | 更新日期: 2023-09-27 18:03:03
我有一个ReadOnly DataGridView绑定到一个bindingSource。表单上还有其他更新DataGridViewCells的控件——它们绑定到bindingsource。我需要根据源的DataViewRowState来改变datagridviewCell样式- . current对我没有帮助-我需要做的是跟踪是cell的原始值-将cell正常着色,如果它被修改为蓝色。我知道我可以得到DataRowViewState给我的版本,我得到-但它总是回来当前(因为它是默认的),我想要当前-我只是想知道它是否不同于原始或不为这个特定的列。由于在我编辑的bindingsource中,我已经有了行,因此似乎没有一个简单的方法或属性来表示这行是原始的或修改的。我是否需要在表上运行一个使用filterstates的选择来获取我的行,看看我是否得到结果,然后如果是这样,改变单元格,以便做到这一点?
相关代码:-我知道它是在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
查看评论并使用不同的想法搜索后,我发现了一个SO答案,提供了解决方案的一部分,我如何获得数据集行的特定版本?
为了实现该解决方案,我需要设置一种机制来调用泛型函数,其中T在运行时之前是未知的。在再次看了SO -我找到了一些很好的答案;如何使用反射来调用泛型方法?.
下面是我使用的最终代码,从我的控件更改值实现对数据视图的更改,并根据原始值或建议值对单个单元格应用样式。
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)
帮助我的相关链接
如何获得数据集行的特定版本?
如何使用反射来调用泛型方法?