In this tutorial, you will learn how True DBGrid interacts with other bound controls and with code that manipulates the same DataSet to which the grid is bound.
Complete the following steps:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Me.CustomersTableAdapter.Fill(Me.DsCustomers.Customers) |
To write code in C#
C# |
Copy Code
|
---|---|
this.CustomersTableAdapter.Fill(this.DsCustomers.Customers); |
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click ' Move to the first record in the grid. Me.C1TrueDBGrid1.MoveFirst() End Sub Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click ' Move to the next record in the grid. Me.C1TrueDBGrid1.MoveNext() End Sub Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click ' Move to the previous record in the grid. Me.C1TrueDBGrid1.MovePrevious() End Sub Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click ' Move to the last record in the grid. Me.C1TrueDBGrid1.MoveLast() End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private void btnFirst_Click(System.object sender, System.EventArgs e) { // Move to the first record in the grid. this.c1TrueDBGrid1.MoveFirst(); } private void btnNext_Click(System.object sender, System.EventArgs e) { // Move to the next record in the grid. this.c1TrueDBGrid1.MoveNext(); } private void btnPrevious_Click(System.object sender, System.EventArgs e) { // Move to the previous record in the grid. this.c1TrueDBGrid1.MovePrevious(); } private void btnLast_Click(System.object sender, System.EventArgs e) { // Move to the last record in the grid. this.c1TrueDBGrid1.MoveLast(); } |
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click ' Delete the record and save the changes to the database. Me.C1TrueDBGrid1.Delete() Call UpdateCustomers() End Sub Private Sub BtnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click ' Update the grid, call UpdateCustomers to save. Me.C1TrueDBGrid1.UpdateData() Call UpdateCustomers() End Sub Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click ' This "Add New" button moves the cursor to the "new (blank) row" at the end so that user can start adding data to the new record. ' Move to last record, "new row" will be visible. Me.C1TrueDBGrid1.MoveLast() ' Move to the "addnew row", and set focus to the grid. Me.C1TrueDBGrid1.Row = Me.C1TrueDBGrid1.Row + 1 Me.C1TrueDBGrid1.Select() End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private void btnDelete_Click(System.object sender, System.EventArgs e) { // Delete the record and save the changes to the database. this.c1TrueDBGrid1.Delete(); UpdateCustomers(); } private void BtnUpdate_Click(System.object sender, System.EventArgs e) { // Update the grid, call UpdateCustomers to save. this.c1TrueDBGrid1.UpdateData(); UpdateCustomers(); } private void BtnAdd_Click(System.object sender, System.EventArgs e) { // This "Add new" button moves the cursor to the "new (blank) row" at the end so that user can start adding data to the new record. // Move to last record, "new row" will be visible. this.c1TrueDBGrid1.MoveLast(); // Move to the "addnew row", and set focus to the grid. this.c1TrueDBGrid1.Row = this.c1TrueDBGrid1.Row + 1; this.c1TrueDBGrid1.Select(); } |
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub UpdateCustomers() Me.CustomersTableAdapter.Connection.Open() Dim UpdatedRows As System.Data.DataSet Dim InsertedRows As System.Data.DataSet Dim DeletedRows As System.Data.DataSet ' These Data Tables hold any changes that have been made to the dataset since the last update. UpdatedRows = Me.DsCustomers.GetChanges(DataRowState.Modified) InsertedRows = Me.DsCustomers.GetChanges(DataRowState.Added) DeletedRows = Me.DsCustomers.GetChanges(DataRowState.Deleted) Try ' For each of these, we have to make sure that the Data Tables contain any records, otherwise, we will get an error. If Not UpdatedRows Is Nothing Then Me.CustomersTableAdapter.Update(UpdatedRows) If Not InsertedRows Is Nothing Then Me.CustomersTableAdapter.Update(InsertedRows) If Not DeletedRows Is Nothing Then Me.CustomersTableAdapter.Update(DeletedRows) Catch eUpdate As System.Exception MessageBox.Show ("Caught exception: " & eUpdate.Message) End Try Me.CustomersTableAdapter.Connection.Close() End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private void UpdateCustomers() { this.CustomersTableAdapter.Connection.Open(); System.Data.DataSet UpdatedRows; System.Data.DataSet InsertedRows; System.Data.DataSet DeletedRows; // These Data Tables hold any changes that have been made to the dataset since the last update. UpdatedRows = this.DsCustomers.GetChanges(DataRowState.Modified); InsertedRows = this.DsCustomers.GetChanges(DataRowState.Added); DeletedRows = this.DsCustomers.GetChanges(DataRowState.Deleted); try { // For each of these, we have to make sure that the Data Tables contain any records, otherwise, we will get an error. if (! UpdatedRows == null ) this.CustomersTableAdapter.Update(UpdatedRows) if (! InsertedRows == null ) this.CustomersTableAdapter.Update(InsertedRows) if (! DeletedRows == null ) this.CustomersTableAdapter.Update(DeletedRows) } catch (System.Exception eUpdate) { MessageBox.Show ("Caught exception: " + eUpdate.Message); } this.CustomersTableAdapter.Connection.Close(); } |
This concludes this tutorial; you've successfully completed interacting with code and other bound controls.