Results 1 to 7 of 7

Thread: [2005] Datagridview Click event question

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    4

    [2005] Datagridview Click event question

    I browsed through the forums and did not see a topic like this, if I missed it please point it out to me. Here is my situation. I have a form with several textboxes that reflect the columns of my SQL database. I also have a search function which opens a second form which has a datagridview that displays the query results. The first column of this DGV is the Primary key AND the record number found on the bindingnavigator from form one. What I'd would like to do is when a row is clicked in the DGV, is to navigate to the appropriate record on form1. Would anyone have an example on how to do this?

    Thank you in advance.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Datagridview Click event question

    In your second form, declare a public property that returns the ID of the record selected. If the dialogue returns OK, read that property value and pass it to the BindingSource's Find method to find the index of the record with that ID. You then set the BindingSource's Position property to the index. E.g.
    vb.net Code:
    1. Using dlg As New DialogueForm
    2.     If dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
    3.         Me.BindingSource1.Position = Me.BindingSource1.Find("ID", dlg.SelectedID)
    4.     End If
    5. End Using
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    4

    Re: [2005] Datagridview Click event question

    Wow, I got a few questions at this point.

    If I declare a public property on my second form, am I creating a third form as the new dialogueform?
    My public property is expecting an identifier, I never used one before, so I am completely at a loss on this one.
    Is this in the "set" part of the public property? i.e.

    Code:
    public property '<----Indentifier expected
            Get
    
            End Get
            Set(ByVal value)
                Using dlg As New DialogueForm
                    If dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
                        Me.BindingSource1.Position = Me.BindingSource1.Find("ID", dlg.SelectedID)
                    End If
                End Using
            End Set
        End Property
    I apologize for the noob type questions as this is my first project with VB and SQL.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Datagridview Click event question

    You're not supposed to be displaying the dialogue in the property. SelectedID is the property you're supposed to declare in the second form. That code is supposed to be in the first form and it gets the value of the SelectedID property from the second form.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    4

    Re: [2005] Datagridview Click event question

    Thank you for the reply. I found a pretty easy way to accomplish the same thing. I couldn't have done it without the idea you gave me, so I thank you for that

    Code:
    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
            Dim tst As Integer
            
            DataGridView1.ReadOnly = True
            tst = DataGridView1.CurrentRow.Cells(0).Value - 1
            Form1.PraiseBindingSource.Position = tst
        End Sub

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Datagridview Click event question

    Hmmm... default instances. If you're going to use default instances then you'd better understand what they are because if you don't display a default instance in the first place then making changes to a default instance won't help you. It works in this case because your startup form is the default instance of its type but if you try to access Form2 that way from Form1, for instance, it will not work unless its the default instance of Form2 that you displayed in the first place.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    4

    Re: [2005] Datagridview Click event question

    I think I got what you mean. Yes form1 always loads the default instance programatically. Form2 is only accessed in a button.click event as a search engine (more or less)(New dynamic dataset pulled from the database itself). The results are shown in form2 and form2 is only opened in that button event. Does that make any sense?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width