Articles in this section
Category / Section

How to Search and Select Record in WinForms DataGrid?

1 min read

Search and select record

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

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

 

Show the search and select the record in datagrid

Sample: How to search and select record in datagrid?

Reference link: https://help.syncfusion.com/windowsforms/datagrid/search


Conclusion

I hope you enjoyed learning about how to search and select record in WinForms DataGrid.

You can refer to our WPF DataGrid featuretour page to know about its other groundbreaking feature representations and documentation, to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or  feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please  to leave a comment
Access denied
Access denied