ComponentOne True DBGrid for .NET (2.0) Search HelpCentral 

To search for entries in a column using an incremental search, add a Timer component to the form, then set the KeyPress and Tick events.

1.   Add a Timer component from the Toolbox to the form.

2.   Set the Timer's Interval property to 1 second.

In the Designer

Locate the Interval property for Timer1 in the Properties window and set it to 1000.

In Code

Add the following code to the Form_Load event:

·      Visual Basic

Me.Timer1.Interval = 1000

·      C#

this.timer1.Interval = 1000;

·      Delphi

Self.Timer1.Interval := 1000;

3.   Declare the search string variable at the form level:

·      Visual Basic

Dim searchString As String = String.Empty

·      C#

string searchString = string.Empty;

·      Delphi

searchString := Empty;

4.   Add the KeyPress event:

·      Visual Basic

Private Sub C1TrueDBGrid1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles C1TrueDBGrid1.KeyPress

 

    ' Handle the keystroke.

    e.Handled = True

 

    Me.searchString += e.KeyChar

    Dim count As Integer = Me.C1TrueDBGrid1.Splits(0).Rows.Count

    Dim start As Integer = Me.C1TrueDBGrid1.Row

    Dim current As Integer = (start + 1) Mod count

 

    ' Stop if search returns to the starting position.

    While current <> start

 

        ' Get the value.

        Dim s As String = Me.C1TrueDBGrid1(current, Me.C1TrueDBGrid1.Col).ToString()

 

        ' If a match is found, exit.

        If s.Substring(0, Me.searchString.Length).ToUpper() = Me.searchString.ToUpper() Then

            Exit While

        End If

 

        ' Search the next row, wrapping the column if needed.

        current = (current + 1) Mod count

    End While

 

    ' Update the grid's current row.

    Me.C1TrueDBGrid1.Row = current

 

    ' Highlight the entry.

    Me.C1TrueDBGrid1.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.HighlightCell

 

    ' Clear the search string at 1 second.

    Me.Timer1.Enabled = True

End Sub

·      C#

private void c1TrueDBGrid1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

{

    // Handle the keystroke.

    e.Handled = true;

 

    this.searchString += e.KeyChar;

    int count = this.c1TrueDBGrid1.Splits[0].Rows.Count;

    int start = this.c1TrueDBGrid1.Row;

    int current = (start + 1) % count;

 

    // Stop if search returns to the starting position.

    while( current != start )

    {

        // Get the value.

        string s = this.c1TrueDBGrid1[current, this.c1TrueDBGrid1.Col].ToString();

 

        // If a match is found, exit.

        if( s.Substring(0, this.searchString.Length).ToUpper() == this.searchString.ToUpper() )

            break;

 

        // Search the next row, wrapping the column if needed.

        current = (current + 1) % count;

    }

 

    // Update the grid's current row.

    this.c1TrueDBGrid1.Row = current;

 

    // Highlight the entry.

    this.c1TrueDBGrid1.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.HighlightCell;

 

    // Clear the search string at 1 second.

    this.timer1.Enabled = true;

}

·      Delphi

procedure C1TrueDBGrid1_KeyPress(sender: object; e: System.Windows.Forms.KeyPressEventArgs);

var

  count : Integer;

  start : Integer;

  current : Integer;

  s : String;

begin

  // Handle the keystroke.

  e.Handled := True;

 

  count := Self.C1TrueDBGrid1.Splits[0].Rows.Count;

  start := Self.C1TrueDBGrid1.Row;

  current := ((start + 1) mod count);

 

  // Stop if search returns to the starting position.

  while (current <> start) do

  begin

    // Get the value.

    s := Self.C1TrueDBGrid1[current, Self.C1TrueDBGrid1.Col].ToString;

 

    // If a match is found, exit.

    if (s.Substring(0, Self.searchString.Length).ToUpper = Self.searchString.ToUpper) then

      end;

 

    // Search the next row, wrapping the column if needed.

    current := ((current + 1) mod count);

  end;

 

  // Update the grid's current row.

  Self.C1TrueDBGrid1.Row := current;

 

  // Highlight the entry.

  Self.C1TrueDBGrid1.MarqueeStyle := C1.Win.C1TrueDBGrid.MarqueeEnum.HighlightCell;

 

  // Clear the search string at 1 second.

  Self.timer1.Enabled := True;

end;

5.   Add the Tick event for the timer:

·      Visual Basic

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    Me.searchString = String.Empty

    Me.Timer1.Enabled = False

End Sub

·      C#

private void timer1_Tick(object sender, System.EventArgs e)

{

    this.searchString = string.Empty;

    this.timer1.Enabled = false;

}

·      Delphi

procedure Timer1_Tick(sender: object; e: System.EventArgs);

begin

  Self.searchString := Empty;

  Self.timer1.Enabled := False;

end;

This topic illustrates the following:

As the user types, the search will highlight the cell containing that letter. In this example, tying V in the Last column highlights Varese.

If more than one entries begin with the same letter, typing the next letter will highlight the entry with those letters. For example, typing Viv in the Last column will highlight Vivaldi.

Note: After 1 second, the search string will reset.


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