Skip to content

SyncfusionExamples/How-to-search-and-select-record-in-winforms-datagrid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to Search and Select Record in WinForms DataGrid?

This example illustrates how to select the record that matches the text typed in a TextBox by using the searching option in the WinForms DataGrid (SfDataGrid).

You can search and select a record in DataGrid based on the searched text by using the TextChanged event of TextBox by calling SearchController.Search method.

C#

private void textBox1_TextChanged(object sender, System.EventArgs e)
{
    this.sfDataGrid.SearchController.Search(this.textBox1.Text);
}
 
private void nextButton_Click(object sender, System.EventArgs e)
{
    this.sfDataGrid.SearchController.FindNext(this.textBox1.Text);
    SetSelectedItem();
}
 
private void previousButton_Click(object sender, System.EventArgs e)
{
    this.sfDataGrid.SearchController.FindPrevious(this.textBox1.Text);
    SetSelectedItem();
}
 
private void SetSelectedItem()
{
    var rowIndex = this.sfDataGrid.SearchController.CurrentRowColumnIndex.RowIndex;
    var recordIndex = this.sfDataGrid.TableControl.ResolveToRecordIndex(rowIndex);
    this.sfDataGrid.SelectedIndex = recordIndex;
}

VB

Private Sub textBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles textBox1.TextChanged
    Me.sfDataGrid.SearchController.Search(Me.textBox1.Text)
End Sub
 
Private Sub nextButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
    Me.sfDataGrid.SearchController.FindNext(Me.textBox1.Text)
    SetSelectedItem()
End Sub
 
Private Sub previousButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button2.Click
    Me.sfDataGrid.SearchController.FindPrevious(Me.textBox1.Text)
    SetSelectedItem()
End Sub
 
Private Sub SetSelectedItem()
    Dim rowIndex = Me.sfDataGrid.SearchController.CurrentRowColumnIndex.RowIndex
    Dim recordIndex = Me.sfDataGrid.TableControl.ResolveToRecordIndex(rowIndex)
    Me.sfDataGrid.SelectedIndex = recordIndex
End Sub

How to Search and Select Record in WinForms DataGrid

Take a moment to peruse the WinForms DataGrid - Search documentation, where you can find about DataGrid with code examples.

About

This example explains about how to search and select record in winforms datagrid

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 6