Sample 1:
l BindingSource1 is just like Data1 Control in vb6
l BindingSource.Datasource property is just like the Data1.RecordSource Property in vb6
l TextBox1.DataBindings.Add("Text",BindingSource1,"LastName") is just like the DataSource and DataField
properties of a Textbox in vb6
Imports System.Data.Sqlclient
Public Class Form1
Dim objConn As New SqlClient.SqlConnection
Dim objDs As New DataSet
Dim objCommand As New SqlCommand()
Dim objDa As New SqlDataAdapter
Dim objCommandBuilder As New SqlCommandBuilder()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Step 1. Create a DataSet...
objConn.ConnectionString = "Server=(local);Database=NorthWind;Trusted_Connection=True;"
objCommand.CommandText = "Select * From Employees"
objCommand.Connection = objConn
objDa.SelectCommand = objCommand
objDa.Fill(objDs, "Employees")
objCommandBuilder = New SqlCommandBuilder(objDa)
'Step 2. Set the BindingSource1.DataSource = DataSet...
BindingSource1.DataSource = objDs.Tables("Employees")
'Step 3. Wire up the your field DataBindings....
TextBox1.DataBindings.Add("Text", BindingSource1, "LastName")
'Step 4. Wire up the BindingNavigator...
BindingNavigator1.BindingSource = BindingSource1
End Sub
Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click
'This is how me move the cursor through the dataset....
BindingSource1.MoveNext()
'We still update the dataset this way...
objDa.Update(objDs.Tables("Employees"))
End Sub
End Class
Sample 2:
Imports System.Data.SqlClient
Public Class Form1
Dim objConn As New SqlClient.SqlConnection
Dim objDs As New DataSet
Dim objCommand As New SqlCommand()
Dim objDa As New SqlDataAdapter
Dim objCommandBuilder As New SqlCommandBuilder()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Create your Dataset....
objConn.ConnectionString = "Server=(local);Database=NorthWind;Trusted_Connection=True;"
objCommand.CommandText = "Select * From Employees"
objCommand.Connection = objConn
objDa.SelectCommand = objCommand
objDa.Fill(objDs, "Employees")
objCommandBuilder = New SqlCommandBuilder(objDa)
'Bind to the DataGridView....
BindingSource1.DataSource = objDs.Tables("Employees")
DataGridView1.DataSource = BindingSource1
TextBox1.DataBindings.Add("Text", BindingSource1, "LastName")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Here's how you save the edited DataGridView....
objDa.Update(objDs.Tables("Employees"))
MsgBox("Saved")
End Sub
End Class
Sample 3:
Now for the code….
Imports System.Data.SqlClient
Public Class Form1
Dim objDs As New DataSet()
Dim objDa As New SqlDataAdapter()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim objConn As New SqlConnection("data source=SchimskyPC;Integrated Security=SSPI;Database=AAAAICans")
Dim objCommand As New SqlCommand()
Dim objCommandBuilder As New SqlCommandBuilder()
' **********************************************
objCommand.CommandText = "Select * From Candidates"
objCommand.Connection = objConn
objDa.SelectCommand = objCommand
objDa.Fill(objDs, "Table1")
objCommandBuilder = New SqlCommandBuilder(objDa)
'objDa.FillSchema(objDs, SchemaType.Source, "Table1")
Me.BindingSource1.DataSource = objDs.Tables("Table1")
Me.CandidatesDataGridView.DataSource = Me.BindingSource1
Me.BindingNavigator1.BindingSource = Me.BindingSource1
End Sub
Private Sub ToolStripButton1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
'Save the data...
Me.Validate()
Me.BindingSource1.EndEdit()
Me.objDa.Update(objDs.Tables("Table1"))
MessageBox.Show("Data Saved!")
End Sub
End Class