關于DataGridView的右鍵問題

Daimon 2008-10-16 03:50:28
我在DataGridView設置了右鍵菜單,作用了把選定的記錄導出之類的.
但在DataGridView直接右鍵,它不會先選定鼠標指的那行才出右鍵菜單
而是直接出右鍵菜單,搞得每次都要先左鍵選定,再點右鍵,請問老師們有方法可以點右鍵時先選定再出菜單?
...全文
124 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
一只熊猫 2008-10-16
  • 打赏
  • 举报
回复
正好写了个这样的程序。代码如下:

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
lunber 2008-10-16
  • 打赏
  • 举报
回复

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


我一般加这个函数
kyle2cj 2008-10-16
  • 打赏
  • 举报
回复
需要为Datagridview鼠标单击事件增加代码。。。先判断是否右键单击,是则选中行

16,717

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧