16,717
社区成员
发帖
与我相关
我的任务
分享
Private Sub DataG0_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataG0.MouseDown 'DataG0 is DataGridView
Dim p As Point
p = DataG0.PointToClient(MousePosition)
If e.Button = Windows.Forms.MouseButtons.Right Then
If Not DataG0.Rows(DataG0.HitTest(p.X, p.Y).RowIndex).Selected Then '如果鼠标当前位置的行未选中
DataG0.ClearSelection() '先清除其他可能已选的行
DataG0.Rows(DataG0.HitTest(p.X, p.Y).RowIndex).Cells(3).Selected = True '选择鼠标位置的行
End If
Cmenu1.Show(MousePosition) '弹出定义的右键菜单
End If
End Sub
Private Sub DataGridView1_CellMouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
Dim selectcount As String = DataGridView1.SelectedRows.Count
If selectcount < 2 Then
'限制鼠标右键选择最后一行(空行)
If e.RowIndex >= 0 And e.RowIndex < DataGridView1.RowCount - 1 Then
DataGridView1.ClearSelection()
DataGridView1.Rows(e.RowIndex).Selected = True
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
DataGridView1.CurrentCell = DataGridView1.Rows(e.RowIndex).Cells(2)
End If
End If
End Sub