当用户单击标题以对网格进行排序时,将触发DataGridView.Sorted。如何以编程方式告知单击了哪个列标题?和/或哪个列已分类?
我厌倦了下面的代码,但Sorted事件在click事件之前触发。任何一个小组的Doevents都没有帮助。
我可以将对GreenBar的调用移动到click事件,但这似乎是一个坏主意,因为它取决于触发顺序异常。
Private Sub dgvMAFTrans_ColumnHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvMAFTrans.ColumnHeaderMouseClick msLastMAFHeaderClicked = dgvMAFTrans.Columns(e.ColumnIndex).Name End Sub
Private Sub dgvMAFTrans_Sorted(sender As Object, e As EventArgs) Handles dgvMAFTrans.Sorted
Application.DoEvents()
GreenBar(dgvMAFTrans, msLastMAFHeaderClicked)
End Sub
添加绿条的代码影响:
Sub GreenBar(dgvRef As DataGridView, ColumnName As String)
Dim sLastTrans As String = ""
Dim bAddColor As Boolean = False
If dgvRef.RowCount = 0 Then Exit Sub
Try
sLastTrans = dgvRef.Rows(0).Cells(ColumnName).Value
Catch ex As Exception
Exit Sub ' no transid
End Try
For Each row As DataGridViewRow In dgvRef.Rows
If row.Cells(ColumnName).Value <> sLastTrans Then
bAddColor = Not bAddColor
sLastTrans = row.Cells(ColumnName).Value
End If
If bAddColor Then
row.DefaultCellStyle.BackColor = Color.LightGreen
End If
Next
End Sub