Results 1 to 3 of 3

Thread: BindingNavigator and Bindingsource control

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    92

    BindingNavigator and Bindingsource control

    Hi, I have one datagridview control, a BindingNavigation and a Binding control on my form. I populate the datagridview control through the code. Moreover, the datasource properties of the DatagridView and the Binding controls are assigned a value through code (I didn't specify anyting in the properties window). The same for the bindingsource property of the BindingNavigation control (I assign a value trhough code). My code is the following:

    objDataAdapter.SelectCommand = New SqlCommand()
    objDataAdapter.SelectCommand.Connection = objConnection
    objDataAdapter.SelectCommand.CommandText = _
    "SELECT Code, Description, BarCode, MM, Remainder FROM Table_1 "
    objConnection.Open()
    objDataAdapter.Fill(objDataSet, "Table_1")
    objConnection.Close()

    BindingSource1.DataSource = objDataSet
    DataGridView1.AutoGenerateColumns = True
    DataGridView1.DataSource = objDataSet
    DataGridView1.DataMember = "Table_1"

    BindingNavigator1.BindingSource = BindingSource1()


    This code brings and shows the right results in the datagridview control, but the navigator control is inactive. I know that I have to bind the controls(DataGridView + BindingNavigator) to the BindingSource control. Is this a problem that I tried to do this by coding? Any solutions?

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

    Re: BindingNavigator and Bindingsource control

    You've got multiple problems there. First of all, you're binding the DataSet to the BindingSource instead of the DataTable. If you expect the BindingNavigator to navigate the rows of a DataTable then it has to know which DataTable. You haven't told the BindingSource which DataTable so how can the BindingNavigator know either.

    Second, you're binding the DataTable to the DataGridView instead of binding the BindingSource. Your code should look like this:
    vb.net Code:
    1. 'Bind the DataTable to the BindingSource.
    2. 'ALWAYS specify the DataSource LAST.
    3. BindingSource1.DataMember = "Table_1"
    4. BindingSource1.DataSource = objDataSet
    5.  
    6. 'Associate the BindingNavigator with the BindingSource.
    7. BindingNavigator1.BindingSource = BindingSource1
    8.  
    9. 'Bind the BindingSource to the DataGridView.
    10. DataGridView1.DataSource = BindingSource1
    Finally, you should note that the BindingSource is NOT a control. It is a component. If it doesn't inherit the Control class it's not a control.
    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
    Lively Member
    Join Date
    Dec 2008
    Posts
    92

    Re: BindingNavigator and Bindingsource control

    I really appreciate the help you are giving to me. Now it works.
    I was mistaken because I read in a book that I have to bind the bindingsource to the dataset. But I had to bind it also to the dataTable with the command you wrote me:
    BindingSource1.DataMember = "Table_1"

    and secondly even if I knew that I had to assign the datagridview.datasource to the bindingsource1 I assigned it on the dataset.
    Thank you for the help

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