Thursday 26 January 2012

contextmenu strip when right click on datagridviwe


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.
  1. ·         The following screen shot of contextmenu strip.
  2. ·         When user right click on datagridviwe row that time the strip display and when user click on delete the id of row is display.
  3. ·         You may write code for delete and whatever functionality you write there.
  4. ·         To add contextmenu strip selet toolbox and click on contextmenu strip and add it.
  5. ·         You not need to do any changes in property of contextmenu strip.
  6. 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