Adding a binding navigator with code. VS2005
Hello all,
I have a dataset used to populate a datagrid. I wish to bind a Binding navigator to the datagrid after its created.
I know how to do it using the wizard and with almost no code but i want to do it code wise so i can see what happens in the background. (does that make sence?) here is my simple code.
VB Code:
Imports System.Data
Imports System.Data.SqlClient
Public Class Inventory
'Dim objAdapter As New SqlDataAdapter
Dim objDataset As New DataSet
Private Function getConnectionString() As String
Return "***edited for Safety***"
End Function
Private Function getSqlString() As String
Return "SELECT * FROM EQ_Equipment"
End Function
Private Sub Inventory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Dim sqlConnection As SqlConnection = New SqlConnection(getConnectionString)
Dim objAdapter As New SqlDataAdapter(getSqlString, getConnectionString)
Dim objdataset As New DataSet
objAdapter.Fill(objdataset, "inventory")
dgvInventory.DataSource = objdataset
dgvInventory.DataMember = "inventory"
End Sub
End Class
Re: Adding a binding navigator with code. VS2005
A good way to learn how to code something manually is to use the wizard and then view the system generated code afterwards. That should give you a pretty good idea of what properties need to be set in order to successfully bind the Navigator.
Re: Adding a binding navigator with code. VS2005
You don't bind a BindingNavigator to a DataGridView. Those two objects know absolutely nothing about each other.
1. Populate a DataTable with your data.
2. Bind your DataTable to a BindingSource.
3. Assign your BindingSource object to the BindingSource property of your BindingNavigator.
Now, any actions you perform on the BindingNavigator will affect the data exposed through the BindingSource.
4. Bind your BindingSource to your DataGridView.
Now, the grid will display the data exposed through the BindingSource.
The BindingSource is the centre of all this. The BindingNavigator affects the data in the BindingSource and the grid displays the data in the BindingSource. The BindingNavigator and the grid never meet.