首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

DataGrid1鼠标点击事件解决方案

2012-01-08 
DataGrid1鼠标点击事件有没有这样的事件,在鼠标双击某数据行的时候发生datagrid.doubleclick事件时只要在

DataGrid1鼠标点击事件
有没有这样的事件,在鼠标双击某数据行的时候发生

datagrid.doubleclick事件时只要在控件内双击就会发生,如何判断用户双击位置是在某数据行而不是在caption?

[解决办法]
' e.x e.y 

Private Function GetRowFromPoint(ByVal x As Integer, ByVal y As Integer, ByVal tg As Windows.Forms.DataGridView) As Integer()
Dim i As Integer
Dim j As Integer
Dim k(2) As Integer

For i = 0 To tg.Rows.Count - 1
For j = 0 To tg.ColumnCount - 1
Dim rec As Drawing.Rectangle
rec = tg.GetCellDisplayRectangle(i, j, True)
If (tg.RectangleToScreen(rec).Contains(x, y)) Then
k(0) = i
k(1) = j

Return k
End If
Next

Next

Return k
End Function
 
Private Sub DataGridView1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDoubleClick
If e.Clicks < 2 And e.Button = Windows.Forms.MouseButtons.Left Then
Dim idx() As Integer

idx = GetRowFromPoint(e.X, e.Y, sender)

'idx(0) 为鼠标所在行,'idx(1)为鼠标所在列 
'之后代码应该知道怎么写了吧

End If
End Sub
[解决办法]
dim fgRow ,fgCol as Integer
fgRow = CType(yourdatagird, DataGird).MouseRow
fgCol = CType(yourdatagird, DataGird).MouseCol
If fgRow > 0 Then
yourdatagird.Select(fgRow, fgCol)
ctxMnuFrom.Show(yourdatagird, New Point(e.X, e.Y))
End If

热点排行