ComponentOne True DBGrid for WinForms
True DBGrid for WinForms Tutorials / Tutorial 4: Interacting with Code and Other Bound Controls
In This Topic
    Tutorial 4: Interacting with Code and Other Bound Controls
    In This Topic

    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:

    1. Create a new .NET project.
    2. Place the following controls on the form (Form1) as shown in the figure below:
      • C1TrueDBGrid control (C1TrueDBGrid1)
      • ListBox control (ListBox1)
      • Three text controls (TextBox1 to 3)
      • Seven buttons (Named 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 C1TrueDBGrid 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 C1NWind.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 :

      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);
      
    5. Now for each of the four Buttons (Button4, 5, 6, 7) on the lower right in the above image, add the following code:

      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();
      }
      
    6. Set the code for the three buttons on the upper right in the above image:

      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();
      }
      
    7. Now add the UpdateCustomers subroutine. It sends information back to the DataSet from temporary DataTables:

      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();
      }
      
    8. Now back in the .NET designer, click 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 C1TrueDBGrid control.

    Run the program and observe the following:

    This concludes this tutorial; you've successfully completed interacting with code and other bound controls.