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?
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:
'Bind the DataTable to the BindingSource.
'ALWAYS specify the DataSource LAST.
BindingSource1.DataMember = "Table_1"
BindingSource1.DataSource = objDataSet
'Associate the BindingNavigator with the BindingSource.
BindingNavigator1.BindingSource = BindingSource1
'Bind the BindingSource to the DataGridView.
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.
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