Solved

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

Posted on 2010-11-15
2
498 Views
Last Modified: 2012-05-10
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.
0
Comment
Question by:roarteam
2 Comments
 
LVL 7

Accepted Solution

by:
rogerard earned 500 total points
ID: 34141844
If you change the contextmenu to a contextmenustrip, then you can assign it to the datagrid in the properties tab and then you can let .net handle the right click to show the menu.  Just a thought.

As for passing the information, in the form you want to open, create a
sub New (byval row as datagridrow)
... your code here to initialize ...
end sub

Then

   Private Sub OpenFormToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenFormToolStripMenuItem.Click
        dim newform as new MyCustomform(datagrid1.SelectedRows(0))
        newform.show
    End Sub
0
 

Author Comment

by:roarteam
ID: 34147039
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.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question