Hi all,

I've only just stared using VS2008 VB so please excuse me if this seems a very basic question. (I've amended my example to the Northwind Database to avoid any confusion)


WHAT I HAVE
I have a dataset called KeyData made up of two tables Customers and Orders.

I have two forms, one named MainForm that shows records from Customers in details view and the related Orders shown in GridView called OrdersDataGridView. The other form is called OrdersForm and is populated with the Orders table from the keydata dataset and shows records in details view.

When I navigate the MainForm customer records the correct orders are shown in the OrdersDataGridView. So far so good.

Here's the code that was generated when I dropped the tables within KeyData dataset onto the MainForm:

Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'KeyData.Orders' table. You can move, or remove it, as needed.
        Me.OrdersTableAdapter.Fill(Me.KeyData.Orders)
        'TODO: This line of code loads data into the 'KeyData.Customers' table. You can move, or remove it, as needed.
        Me.CustomersTableAdapter.Fill(Me.KeyData.Customers)
    End Sub

WHAT I WANT.
On clicking a recorded (or row) in OrdersDataGridView I want to open the second from (OrdersForm) and only show derails (in the OrdersForm) of the specific row that was clicked on the OrdersDataGridView. I'm sure everyone knows the related field in Orders in OrdersID

As I'll be opening the OrdersForm as a modal I don't need to close the MainForm.

Also, I’ll be using this method of showing details in various other parts my project. In some case I’ll want to show records that can be edited, in other cases I want the records to be read only and in one instance I’ll want to show a new record ready for data entry. In this case I want them to be read only, but it would be nice to know how I could change this on opening the form.

Sorry to be so long winded in the explanation but I wanted to be as clear as my knowledge allows.

Thank you in advance

PS For thos who surf the forums I did post this elsewhere but haven't got much help so far.

Recommended Answers

All 6 Replies

Hmm, No answers.
Maybe I've over-cooked the question.

Let me rephrase.
When I click a row in gridview how do I open another form and show a related record based on the common field OrdersID?

Thanks

I have same proplem

I hope there is somebody help us...

Hope my friend, hope.

Hi,

You should use showdialog method for open the second form. You should make a property for second form, so you can give the actual OrderID through this property. On the second form you need to do data filtering (this is the easiest, but not so elegant), or a special SQL select method for get the correct details.

I hope, I could help you.

I hope this will help you out:

Set SelectionMode to FullRowSelect

Private Sub myGridview_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles myGridview.SelectionChanged

        Dim gv As DataGridView = CType(sender, DataGridView)

        If gv.SelectedRows(0).Index > 0 Then
            Dim row As DataGridViewRow = gv.SelectedRows(0)

            Dim RecID As Integer = CType(row.Cells(0).Value, Integer)

           ' Open new form and pass the record id for the details
        End If

    End Sub

you can use row.Cells(index) or row.Cells("ColumnName")

In your second form make a constructor

Public Sub New(ByVal row As DataRow)
      Me.InitializeComponent()

      'Add the other initialization here...

End Sub

so when calling your second form use the constructor you made and pass the row you haw selected

Dim frm2 As New Form2
frm2.Show(DataGridView1.CurrentRow)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.