Visual Basic, Open Form based on row value

Posted on 2012-09-21
Last Modified: 2012-09-24
Hi Everyone,

I have an interface that looks at related rows from different tables. Basically, it has a form/sub-form relationship.

The interface allows them to select a single row from a Datagridview. What I want, is for a second form to open based on the row selection in the first form. It would open the same row in a second form that looks at just that table. (The primary key is: GoalRowID). Also, the row in the second form contains more columns for the table.

Here is some of my incomplete code:
    Private Sub EditRowButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditRowButton.Click

        Dim RecordIDString As String

        If  DataGridView1.SelectedRows.Count > 1 Then
            MessageBox.Show("You can only select one row at a time")

        End If
    End Sub

Open in new window

Question by:jonsuns7
    1 Comment
    LVL 83

    Accepted Solution

    In the else part, you need to get the primary key ID from selected row using code like below

    Dim ID as integer = Datagridview1.SelectedRows(0).Cells("GoalRowID").Value


    Form2.GoalRowID = ID

    You will need to declare GoalRowID as a public property on Form2 so you can pass it from form1. Then in form2 load, use that ID to load data from DB.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now