Link to home
Start Free TrialLog in
Avatar of roarteam
roarteam

asked on

OPening a form and passing data from datagrid in VB.NET

I have a form (frmMain) with a datagrid (dgCustomerMain).  I also created a contextmenu (contextCustomers) with a selection of "View Customer Detail".  I would like to right click on a record to show the context menu, select the "View Customer Detail" item and then open up an existing form (frmCustomerDetail) to that particular record for editing.  I have searched and tried similar posts, but they are either in C or only create a msgbox.

Currently, I have the right click code working:
    Private Sub dgCustomerMain_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgCustomerMain.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Right Then
 
        End If
    End Sub

Now I just need the code to display the context and open the form to the correct record.
ASKER CERTIFIED SOLUTION
Avatar of rogerard
rogerard
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of roarteam
roarteam

ASKER

Here's what worked..

In the form load:
 
Me.CustomerTBLTableAdapter.Fill(Me.CustomerDataSet.CustomerTBL)
        With Me.dgCustomerMain
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .MultiSelect = False
        End With

Open in new window

That selects the entire row anytime a cell is clicked

To pick which row to select:
 
Private Sub dgCustomerMain_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgCustomerMain.CellMouseDown
        If (e.Button = Windows.Forms.MouseButtons.Right) Then
            Debug.WriteLine("In CellMouseDown")
            dgCustomerMain.ClearSelection()
            dgCustomerMain.Rows(e.RowIndex).Selected = True
            dgCustomerMain.CurrentCell = dgCustomerMain.Rows(e.RowIndex).Cells(1)
        End If
    End Sub

Open in new window


 
To open the form and pass a value
 
Private Sub dgCustomerMain_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgCustomerMain.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Dim grid As DataGridView = sender
            Dim menu As ContextMenuStrip = New ContextMenuStrip
            Dim pt As Point = grid.PointToClient(Control.MousePosition)
            Dim id As Integer = Convert.ToInt16(Me.dgCustomerMain.SelectedCells(0).Value.ToString)
            Me.contextCustomers.Show(dgCustomerMain, pt.X, pt.Y)
        End If
    End Sub

Open in new window


And in the form being opened...
 
Public Sub New(ByVal idNum As Integer)
        InitializeComponent()
        Debug.WriteLine("ID passed = " & idNum)
        Dim row As Integer
        row = CustomerTBLBindingSource.Find("CustomerID", row)
        Debug.WriteLine("Index=" & row)
        If row <> -1 Then
            CustomerTBLBindingSource.Position = row
        End If

Open in new window


The find is not working, but I'll leave that to another post.