ComponentOne True DBGrid for .NET (2.0) Search HelpCentral 

Tutorial 5 - Selecting Multiple Rows Using Bookmarks

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.

1.   Start a new .NET project.

2.   From the Toolbox on the left side of the IDE draw two command buttons and the C1TrueDBGrid object 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 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 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 :

·      Visual Basic

Me.ComposerTableAdapter.Fill(Me.DsComposer.Composer)

·      C#

this.ComposerTableAdapter.Fill(this.DsComposer.Composer);

·      Delphi

Self.ComposerTableAdapter.Fill(Self.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:

·      Visual Basic

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

·      C#

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

·      Delphi

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

var

  I : Integer;

begin

  for I := 0 to Self.DsComposer.Composer.Rows.Count – 1 do

  begin

    if (Self.DsComposer1.Composer.Rows[i].Item[‘Country’].ToString = ‘Germany’) then

      Self.C1TrueDBGrid1.SelectedRows.Add(i);

  end;

end;

Self.C1TrueDBGrid1.Refresh;

7.   To deselect rows, add the following code to the Click event of Button2:

·      Visual Basic

Me.C1TrueDBGrid1.SelectedRows.Clear()

·      C#

this.C1TrueDBGrid1.SelectedRows.Clear();

·      Delphi

Self.C1TrueDBGrid1.SelectedRows.Clear;

Run the program and observe the following:

·      C1TrueDBGrid1 retrieves the database schema information from the DataSet and automatically configures itself to display all of the fields in the joined database tables. This is again similar to the behavior of the grid in Tutorial 1.

·      Click the Select button and observe that all records with the Country field equal to Germany will be highlighted.

·      To deselect the highlighted records, click the Clear button. Alternatively, clicking anywhere on a grid cell will also clear the selected rows.

This concludes the tutorial.


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