Results 1 to 5 of 5

Thread: Fill textboxes by clicking on DGV using BindingSource

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Fill textboxes by clicking on DGV using BindingSource

    From this thread I kind of get the idea of how to fill textboxes with records from a row by clicking on the datagridview but I have not quite got it right in practice.

    My Code:
    VB Code:
    1. Private Sub frmEdit_Load( _
    2.         ByVal sender As Object, _
    3.         ByVal e As System.EventArgs) Handles Me.Load
    4.  
    5.         Dim da As SqlDataAdapter
    6.         Dim ds As DataSet = New DataSet
    7.         da = New SqlDataAdapter("Select * " & _
    8.            "From vShowEditData Where StoreID = '" & _
    9.            StoreID & "' Order by CategoryCode, ItemCode", Con)
    10.         da.TableMappings.Add("Table", "EditInv")
    11.         Try
    12.             da.Fill(ds)
    13.             ds.Tables("EditInv").DefaultView.AllowNew = False
    14.             dgvEditData.DataSource = ds.Tables("EditInv")
    15.             myBindingSource.DataSource = ds.Tables("EditInv")
    16.         Catch ex As Exception
    17.             MessageBox.Show( _
    18.                 ex.Message & vbNewLine & _
    19.                 ex.Source, "Data Error", _
    20.                 MessageBoxButtons.OK)
    21.         Finally
    22.             da.Dispose()
    23.             ds.Dispose()
    24.         End Try
    25.     End Sub
    26.  
    27.     Private Sub dgvEditData_RowHeaderMouseDoubleClick( _
    28.         ByVal sender As Object, _
    29.         ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) _
    30.         Handles dgvEditData.RowHeaderMouseDoubleClick
    31.  
    32.         tbStart.DataBindings.Add("Text", myBindingSource, "OnHandQ")
    33.     End Sub
    On the first dbl Click it sets it to 0 every time and on the second dbl Click I get this error: This causes two bindings in the collection to bind to the same property. Parameter name: binding

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

    Re: Fill textboxes by clicking on DGV using BindingSource

    This is wrong:
    vb.net Code:
    1. dgvEditData.DataSource = ds.Tables("EditInv")
    2. myBindingSource.DataSource = ds.Tables("EditInv")
    It should be like this:
    vb.net Code:
    1. myBindingSource.DataSource = ds.Tables("EditInv")
    2. dgvEditData.DataSource = myBindingSource
    Also, you should be setting the AllowNew property of the BindingSource, not the DefaultView.

    If you want your TextBoxes, etc., to be populated with the same record's data when you select a row in the grid then you need to bind those TextBoxes, etc., to the same BindingSource as the grid.
    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
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Fill textboxes by clicking on DGV using BindingSource

    Got it. Thanks. The only issue I have now is that if I select a second row I get the error mentioned above. Do I need to clear the databinding before I collect the row data (each time)? I tried DataBindings.Clear() But I get the same error.

    VB Code:
    1. Private Sub frmEdit_Load( _
    2.         ByVal sender As Object, _
    3.         ByVal e As System.EventArgs) Handles Me.Load
    4.  
    5.         Dim da As SqlDataAdapter
    6.         Dim ds As DataSet = New DataSet
    7.         da = New SqlDataAdapter("Select * " & _
    8.            "From vShowEditData Where StoreID = '" & _
    9.            StoreID & "' Order by CategoryCode, ItemCode", Con)
    10.         da.TableMappings.Add("Table", "EditInv")
    11.         Try
    12.             da.Fill(ds)
    13.             myBindingSource.DataSource = ds.Tables("EditInv")
    14.             dgvEditData.DataSource = myBindingSource
    15.             myBindingSource.AllowNew = False
    16.         Catch ex As Exception
    17.             MessageBox.Show( _
    18.                 ex.Message & vbNewLine & _
    19.                 ex.Source, "Data Error", _
    20.                 MessageBoxButtons.OK)
    21.         Finally
    22.             da.Dispose()
    23.             ds.Dispose()
    24.         End Try
    25.     End Sub
    26.  
    27.     Private Sub dgvEditData_RowHeaderMouseDoubleClick( _
    28.         ByVal sender As Object, _
    29.         ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) _
    30.         Handles dgvEditData.RowHeaderMouseDoubleClick
    31.         DataBindings.Clear()
    32.         tbStart.DataBindings.Add("Text", myBindingSource, "OnHandQ")
    33.     End Sub

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Fill textboxes by clicking on DGV using BindingSource

    jmcilhinney how do I make those text boxes change dynamically as I click on the grid? As it is now it complains after I choose a second row.

    ---------------------------
    Data Error
    ---------------------------
    This causes two bindings in the collection to bind to the same property.

    Parameter name: binding

    System.Windows.Forms
    ---------------------------
    OK
    ---------------------------

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

    Re: Fill textboxes by clicking on DGV using BindingSource

    Like I said, you bind the TextBoxes to the same BindingSource, e.g.
    vb.net Code:
    1. myBindingSource.DataSource = myDataTable
    2. myDataGridView.DataSource = myBindingSource
    3. myTextBox.DataBindings.Add("Text", myBindingSource, "SomeColumn")
    When you select a record in the grid the Current property of the BindingSource is updated, which will in turn update the bound TextBox, as well as any other controls bound to the same source. It's all automatic once the bindings are set.
    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

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