ComponentOne True DBGrid for WinForms
True DBGrid for WinForms Tutorials / Tutorial 5: Selecting Multiple Rows Using Bookmarks
In This Topic
    Tutorial 5: Selecting Multiple Rows Using Bookmarks
    In This Topic

    In this tutorial, you will learn how to select and highlight records that satisfy specified criteria. A group of similar items is generally implemented as a collection in True DBGrid. When manipulating a group of items in True DBGrid, use techniques similar to those described here.

    Complete the following steps:

    1. Create a new .NET project.
    2. From the Toolbox on the left side of the IDE add two command buttons and a C1TrueDBGrid control onto the form. The C1TrueDBGrid icon looks like this:
    3. Set Button1's Text property to "Select" and set Button2's Text property to "Clear."
    4. 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 Composers table and type "DsComposers" into the DataSet name box, and then finish out the wizard.
    5. Visual Studio adds the following code to the Form_Load event :

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.ComposerTableAdapter.Fill(Me.DsComposer.Composer)
      

      To write code in C#

      C#
      Copy Code
      this.ComposerTableAdapter.Fill(this.DsComposer.Composer);
      
    6. We can easily select and deselect rows in True DBGrid by manipulating the SelectedRowCollection. To select rows, add the following code to the Click event of Button1:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Dim l As Integer
      For l = 0 To Me.DsComposer.Composer.Rows.Count - 1
          If Me.DsComposer.Composer.Rows(l).Item("Country") = "Germany" Then
              Me.C1TrueDBGrid1.SelectedRows.Add(l)
          End If
      Next
      Me.C1TrueDBGrid1.Refresh()
      

      To write code in C#

      C#
      Copy Code
      int l;
      for (l = 0 ; l < this.DsComposer.Composer.Rows.Count; l++)
      {
          if (this.DsComposer.Composer.Rows[l].["Country"] == "Germany")
          {
              this.c1TrueDBGrid1.SelectedRows.Add(l);
          }
      }
      this.c1TrueDBGrid1.Refresh();
      
    7. To deselect rows, add the following code to the Click event of Button2:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.C1TrueDBGrid1.SelectedRows.Clear()
      

      To write code in C#

      C#
      Copy Code
      this.c1TrueDBGrid1.SelectedRows.Clear();
      

    Run the program and observe the following:

    This concludes this tutorial; you've successfully completed selecting multiple rows using bookmarks.