Display contextmenu strip when right click on datagridviwe
The following code is for display contextmenu strip when
user click on datagridviwe row right click.
It’s very simple to implement.
- · The following screen shot of contextmenu strip.
- · When user right click on datagridviwe row that time the strip display and when user click on delete the id of row is display.
- · You may write code for delete and whatever functionality you write there.
- · To add contextmenu strip selet toolbox and click on contextmenu strip and add it.
- · You not need to do any changes in property of contextmenu strip.
- just add menu in
Dim _SelectionId As Integer = 0
Private Sub
DGListData_MouseDown(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles DGListData.MouseDown
For Each row In DGListData.Rows
row.selected = False
Next
' Load context menu on right mouse click
Dim hitTestInfo As DataGridView.HitTestInfo
If e.Button = Windows.Forms.MouseButtons.Right
Then
hitTestInfo = DGListData.HitTest(e.X, e.Y)
'Set location of new point for display ContextMenuStrip1
Dim x As New Point(Windows.Forms.Cursor.Position.X, _
Windows.Forms.Cursor.Position.Y)
DGListData.Rows(hitTestInfo.RowIndex).Selected = True
'display ContextMenuStrip1 with new locaition.
Me.ContextMenuStrip1.Show(x)
'fetch select row's first colunm value.
_SelectionId = DGListData.Item(0,
DGListData.SelectedRows.Item(0).Index).Value
End If
End Sub
Private Sub
DeleteToolStripMenuItem_Click(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles
DeleteToolStripMenuItem.Click
MsgBox(_SelectionId)
End Sub
No comments:
Post a Comment