[英](C#) DataGridView: How to get Selected Count with cells AND rows?
[英]How to get the changed cells or added rows after Datagridview Sort
我有一个带有数据源的DatagridView,如下所示:
bindingSource = DataTable
dataGridView.DataSource = bindingSource
当编辑单元格或添加新行时,我应用了另一种格式设置样式;如果我对dataGridView进行排序,则单击了列标题之一,则格式设置样式丢失了,所以我的问题是:
在对datagridview排序之后,我如何知道哪些单元格已更改或添加了,所以我可以重新应用格式设置样式
VB .Net或C#可以
最后,我实现了所需的功能,向数据表中添加了一个新的字段/列,并且对该字段中的整个行仅保留一个值,我存储了表示每个单元格的状态/样式的字符串,例如“ ,, E ,, E”因此此示例字符串将指示索引2和5的单元格应具有已编辑的样式。
我在CellValueChanged事件中创建/更改该字符串,示例:
Private Sub DataGridView_CellValueChanged(sender As System.Object, e As DataGridViewCellEventArgs) Handles DataGridView.CellValueChanged
If e.RowIndex = -1 Or IsNothing(DataGridView.CurrentCell) Then
Return
End If
Dim cellPosition As Short = DataGridView.CurrentCell.ColumnIndex
Dim Styles(25) As String
Dim stylesCell = DataGridView.Rows(e.RowIndex).Cells("CellStyleDescriptor").Value
If Not IsDBNull(stylesCell) And _
Not IsNothing(stylesCell) Then
Styles= Split(stylesCell, ",")
End If
If IsDBNull(DataGridView.Rows(e.RowIndex).Cells("Id").Value) Then
For i As Integer = 0 To 25 'New row is being added
Styles(i) = "N"
Next
Else
Styles(cellPosition) = "E" 'Edited/Modified Cell
End If
DataGridView.Rows(e.RowIndex).Cells("CellStyleDescriptor").Value = String.Join(",", String)
End Sub
并读取/应用CellFormatting事件中的样式,示例:
Private Sub DataGridView_CellFormatting(sender As System.Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView.CellFormatting
If e.RowIndex = -1 Or e.ColumnIndex = -1 Then
Return
End If
Dim styles(25) As String
styles = Split(DataGridView.Rows(e.RowIndex).Cells("CellStyleDescriptor").Value, ",")
Select Case styles(e.ColumnIndex)
Case "E" 'Edited cell
e.CellStyle.BackColor = modifiedStyle.BackColor
e.CellStyle.ForeColor = modifiedStyle.ForeColor
Case "N" 'New cell
e.CellStyle.BackColor = newStyle.BackColor
e.CellStyle.ForeColor = newStyle.ForeColor
End Select
End Sub
我希望这可以帮助别人
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.