您的實施是好的。這正是我所做的。最初我試圖處理SetSelected ... Core方法,但細節變得笨拙。我解決了以下問題,因爲1)代碼少,2)不干擾其他代碼,3)簡單。
Public Class DataGridView
Inherits System.Windows.Forms.DataGridView
Protected Overrides Sub OnSelectionChanged(ByVal e As System.EventArgs)
Static fIsEventDisabled As Boolean
If fIsEventDisabled = False Then
If Me.SelectedCells.Count > 1 Then
Dim iColumnIndex As Integer = Me.SelectedCells(0).ColumnIndex
fIsEventDisabled = True
ClearSelection()
SelectColumn(iColumnIndex) 'not calling SetSelectedColumnCore on purpose
fIsEventDisabled = False
End If
End If
MyBase.OnSelectionChanged(e)
End Sub
Public Sub SelectColumn(ByVal index As Integer)
For Each oRow As DataGridViewRow In Me.Rows
If oRow.IsNewRow = False Then
oRow.Cells.Item(index).Selected = True
End If
Next
End Sub
End Class
注意到謝謝,但恐怕表現不會那麼好 – MemoryLeak 2010-03-21 00:55:28
@MemoryLeak:你相信會有一場表演打擊,怎麼樣? – AMissico 2010-03-21 01:50:25