Results 1 to 3 of 3

Thread: Populate Form from DataGridView

  1. #1
    timh is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Nov 2008
    Posts
    40
    Reputation
    57

    Populate Form from DataGridView

    Hello,

    I have a problem with linking a datagridview to text boxes on a form.

    I want to be able to click a row on the dgv and populate the fields of the associated form. Everything works fine, until I filter the dgv or sort it, at which point the relationship between the data on the dgv and the form is lost. I realise that this is because the first row in the dgv only relates to the first record in my dataset up until the point where I sort or filter the dgv, but what I can't get my head round is how to overcome this and achieve what I want to.

    For the unsorted dgv, the code is simple...
    Code:
        Private Sub DataGridView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
              intPtr = DataGridView1.CurrentRow.Index
    
              Call usersubFillEntrantForm(intPtr)
        End Sub
    ..
    ..
        Public Sub usersubFillEntrantForm(ByVal index)
            'sub to fill entrantControl with records from dataset
    
            With eventdataDS.Tables("entrant").Rows(index)
                EntrantControl1.urnLabel.Text = .Item("urn").ToString
                EntrantControl1.surnameTextBox.Text = .Item("surname").ToString
                EntrantControl1.firstnameTextBox.Text = .Item("firstname").ToString
    ..
            End With
        End Sub
    My dataset includes a unique reference number (the "urn" column within the dataset) and I know that somehow I must be able to use this to achieve the result I want. After all, when I designed the dataset I thought the "urn" would come in handy, which is why I put it there. Perhaps it's just because it's late in the evening, but I can't figure it out.

    In case you haven't guessed, I'm an amateur...

    Any tips to resolve this would be greatly appreciated!

    Thanks,

    Tim

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,363
    Reputation
    1544
    You don't use any code at all for this. Data-binding takes care of everything, including sorting and filtering. E.g.
    myDataAdapter.Fill(myDataTable)
    myBindingSource.DataSource = myDataTable
    myDataGridView.DataSource = myBindingSource
    myTextBox.DataBindings.Add("Text", myBindingSource, "ColumnName")
    You can bind as many TextBoxes and other controls as you need for the fields in a record. When you you select a row in the grid, the fields of that row will automatically be displayed in the individual controls. Any edits you make in the individual controls will automatically be reflected in the grid. If you ever want to force pending changes to be committed, e.g. immediately before saving back to the database, you call EndEdit on the BindingSource. because all controls are bound to the BindingSource, they will all respect any changes you make to the Sort and Filter properties of that BindingSource.

  3. #3
    timh is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Nov 2008
    Posts
    40
    Reputation
    57
    Thanks for that!

    I've spent ages coding different routines to get the data from the datagridview onto the form and was quite pleased with myself...only to find there was a built in way to do it!

    Like I say, I'm a complete amateur and as I develop this little application, I am learning new things all the time. Using databindings as you have suggested is making my coding so much easier, though I've got to back track quite a long way now...

    All good fun though. Thanks so much for your help.

Bookmarks

Posting Permissions

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