Public Class mYdATAGRID
Inherits DataGrid
Private m As New ArrayList
Public ReadOnly Property MultiSelectedIndex() As Integer()
Get
Return m.ToArray(GetType(Integer))
End Get
End Property
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
Dim posdg As Point = New Point(e.X, e.Y)
Dim hitDG As DataGrid.HitTestInfo = HitTest(posdg)
If HitDataGrid(hitDG) Then
MyBase.OnMouseDown(e)
End If
End Sub
Private Function HitDataGrid(ByVal Hit As DataGrid.HitTestInfo) As Boolean
Try
If Me.ModifierKeys = Keys.Control Then
If Hit.Row > -1 Then
If m.IndexOf(Hit.Row) > -1 Then
m.Remove(Hit.Row)
Me.UnSelect(Hit.Row)
Else
m.Add(Hit.Row)
Me.Select(Hit.Row)
End If
End If
Return False
Else
m.Clear()
If Hit.Type = DataGrid.HitTestType.RowHeader Then
m.Add(Hit.Row)
End If
Return True
End If
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try
End Function
End Class
如果不符合要求的话,继续发贴
现在是在datagrid里面,点击ctrl键时可以选中多行,但是有一个缺陷,就是当我们不按ctrl键,只是点击了一个cell后,应该把以前的记录都清楚掉,也就是说应该清除这里的m数组。但在这里没有实现,必须在按了其他的键后才可以清除,只是单击该cell时无法清除!希望能给你些帮助!
Dim m As New ArrayList()
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Sqldpr.Fill(D1.test1)
Me.Mydatagrid1.DataSource = D1.test1
Dim style As New DataGridTableStyle()
style.MappingName = "test1"
Dim c2 As New DataGridTextBoxColumn()
c2.MappingName = "id"
c2.HeaderText = "id列"
AddHandler c2.TextBox.KeyDown, AddressOf mykeydown
style.GridColumnStyles.Add(c2)
Dim c1 As New DataGridTextBoxColumn()
c1.MappingName = "name"
c1.HeaderText = "name"
style.GridColumnStyles.Add(c1)
Me.Mydatagrid1.TableStyles.Add(style)
End Sub
Private Sub mykeydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Dim i As Integer
Dim index As Integer = Mydatagrid1.CurrentRowIndex
If e.KeyValue = Keys.ControlKey Then
If m.Contains(index) Then
Mydatagrid1.UnSelect(index)
m.Remove(index)
Else
m.Add(index)
End If
For i = 0 To m.Count - 1
Mydatagrid1.Select(m(i))
Next