VB.net中winForm的dataGrid如何只能选择单行
VB.net中winForm的dataGrid如何只能选择单行
[解决办法]
VB版,写到一个类文件中就能用了。
Option Strict Off
Option Explicit On
Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class MyDataGrid
Inherits DataGrid
Private oldSelectedRow As Integer
'Fields
'Constructors
'Events
'Methods
Public Sub New()
'Warning: Implementation not found
End Sub
Protected Overloads Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
'don 't call the base class if left mouse down
If (e.Button <> Windows.Forms.MouseButtons.Left) Then
MyBase.OnMouseMove(e)
End If
End Sub
Protected Overloads Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
'don 't call the base class if in header
Dim hti As DataGrid.HitTestInfo
hti = Me.HitTest(New Point(e.X, e.Y))
If (hti.Type = DataGrid.HitTestType.Cell) Then
If (oldSelectedRow > -(1)) Then
Me.UnSelect(oldSelectedRow)
End If
oldSelectedRow = -(1)
MyBase.OnMouseDown(e)
Else
If (hti.Type = DataGrid.HitTestType.RowHeader) Then
If (oldSelectedRow > -(1)) Then
Me.UnSelect(oldSelectedRow)
End If
If ((Control.ModifierKeys And Keys.Shift) _
= 0) Then
MyBase.OnMouseDown(e)
Else
Me.CurrentCell = New DataGridCell(hti.Row, hti.Column)
End If
Me.Select(hti.Row)
oldSelectedRow = hti.Row
End If
End If
End Sub
End Class