ComponentOne True DBGrid for .NET (2.0) Search HelpCentral 

Tutorial 4 - Interacting with Code and Other Bound Controls

In this tutorial, you will learn how True DBGrid interacts with other bound controls and with Visual Basic code that manipulates the same DataSet to which the grid is bound.

1.   Start a new .NET project.

2.   Place the following controls on the form (Form1) as shown in the figure:

·      True DBGrid control (C1TrueDBGrid1)

·      ListBox control (ListBox1)

·      Three text controls (TextBox1 to 3)

·      Seven buttons (btnFirst, btnNext, btnPrevious, btnLast, btnDelete, btnUpdate, btnAdd)

·      Four labels (Label1 to 4)

Set the Text properties for each of the labels and buttons seen below:

3.   In the TrueDBGrid Tasks menu, locate the Choose Data Source drop-down and select Add Project Data Source. In the adapter’s Data Source Configuration Wizard, either select a connection to TDBGDemo.mdb or create a new connection to this database. On the Choose your database objects page of the wizard, select all fields in the Customers table and type "DsCustomers" into the DataSet name box, and then finish out the wizard.

4.   Visual Studio adds the following code to the Form_Load event :

·      Visual Basic

Me.CustomersTableAdapter.Fill(Me.DsCustomers.Customers)

·      C#

this.CustomersTableAdapter.Fill(this.DsCustomers.Customers);

·      Delphi

Self.CustomersTableAdapter.Fill(Self.DsCustomers.Customers);

5.   Now for each of the four Buttons (Button4, 5, 6, 7) on the lower right in the above image, add the following code:

·      Visual Basic

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

·      C#

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();

}

·      Delphi

procedure TWinForm.button4_Click(sender: System.Object; e: System.EventArgs);

begin

  // Move to the first record in the grid.

  Self.c1TrueDBGrid1.MoveFirst;

end;

 

procedure TWinForm.button5_Click(sender: System.Object; e: System.EventArgs);

begin

  // Move to the next record in the grid.

  Self.C1TrueDBGrid1.MoveNext;

end;

 

procedure TWinForm.button6_Click(sender: System.Object; e: System.EventArgs);

begin

  // Move to the previous record in the grid.

  Self.C1TrueDBGrid1.MovePrevious;

end;

 

procedure TWinForm.button7_Click(sender: System.Object; e: System.EventArgs);

begin

  // Move to the last record in the grid.

  Self.C1TrueDBGrid1.MoveLast;

end;

6.   Set the code for the three buttons on the upper right in the above image:

·      Visual Basic

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

·      C#

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();

}

·      Delphi

procedure TWinForm.BtnDelete_Click(sender: System.Object; ByVal e As System.EventArgs);

begin

  // Delete the record and save the changes to the database.

  Self.C1TrueDBGrid1.Delete;

  UpdateCustomers;

end;

 

procedure TWinForm.BtnUpdate_Click(sender: System.Object; e: System.EventArgs);

begin

  // Update the grid, call UpdateCustomers to save.

  Self.C1TrueDBGrid1.Update();

  UpdateCustomers;

end;

 

procedure TWinForm.BtnAdd_Click(sender:System.Object; e: System.EventArgs);

begin

  // 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.

  Self.C1TrueDBGrid1.MoveLast;

 

  // Move to the "addnew row", and set focus to the grid.

  Self.C1TrueDBGrid1.Row := Self.C1TrueDBGrid1.Row + 1;

  Self.C1TrueDBGrid1.Select;

End Sub;

7.   Now add the UpdateCustomers subroutine. It sends information back to the DataSet from temporary DataTables:

·      Visual Basic

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

·      C#

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();

}

·      Delphi

procedure TWinForm.UpdateCustomers;

var

  UpdatedRows: System.Data.DataSet;

  InsertedRows: System.Data.DataSet;

  DeletedRows: System.Data.DataSet;

begin

  Self.CustomersTableAdapter.Connection.Open;

 

  // These Data Tables hold any changes that have been made to the dataset since the last update.

  UpdatedRows := Self.DsCustomers.GetChanges(DataRowState.Modified);

  InsertedRows := Self.DsCustomers.GetChanges(DataRowState.Added);

  DeletedRows := Self.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 <> nil) then

      Self.CustomersTableAdapter.Update(UpdatedRows);

    If (InsertedRows <> nil) then

      Self.CustomersTableAdapter.Update(InsertedRows);

    If (DeletedRows <> nil) then

      Self.CustomersTableAdapter.Update(DeletedRows);

  except

    on Ex: System.Exception do MessageBox.Show('Caught exception: ' + Ex.Message);

  end;

  Self.CustomersTableAdapter.Connection.Close;

end;

8.   Now back in the .NET designer, click on the ListBox1 control. In the Properties window set its DataSource property to CustomersBindingSource, and its DisplayMember property to LastName.

9.   Click on the first TextBox (TextBox1) on the form. In the Properties window, expand its DataBindings object. Set the Text property under the DataBindings object to CustomersBindingSource - FirstName. In a similar manner set the same Text property under the associated DataBindings object for the next two TextBoxes. TextBox2 should be set to LastName, and TextBox3 should be set to Company.

10.  Finally in the Properties window set the AllowAddNew, AllowDelete, and AllowUpdate properties to True for the True DBGrid.

Run the program and observe the following:

·      Use the mouse or the keyboard to change the current row position in the grid, and observe the other bound controls (ListBox1 and the TextBoxes) changing their record positions along with the grid, even though they contain no code.

·      Click the Next, Previous, Last, and First buttons and observe that they have the same effects as the corresponding buttons on the Data control.

·      Modify data in a few cells (in the same row) on the grid. Press the Update button. Observe that the modified data has been updated to the database and the pencil icon on the record selector column disappears. Other bound controls on the form now display the modified data.

·      Modify data in one or more of the Text controls. Press the Update or the Next button. The grid will automatically update its data to reflect the new changes.

·      Move the current cell of the grid to any record you wish to delete, then click the Delete button. The record will be deleted and disappears from the grid. The grid automatically moves the current row to the record after the deleted record. Other bound controls on the form also respond by moving their record positions.

This concludes the tutorial.


Send comments about this topic to ComponentOne.
Copyright © ComponentOne LLC. All rights reserved.