|
1 | | -# How to search and select record in datagrid? |
| 1 | +# How to Search and Select Record in WinForms DataGrid? |
2 | 2 |
|
3 | | -## About the example |
| 3 | +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](https://www.syncfusion.com/winforms-ui-controls/datagrid) (SfDataGrid). |
4 | 4 |
|
5 | | -This example illustrates how to select the record that matches the text typed in a textbox by using the searching option in the DataGrid. |
| 5 | +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](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.DataGrid.SearchController.html#Syncfusion_WinForms_DataGrid_SearchController_Search_System_String_) method. |
6 | 6 |
|
| 7 | +### C# |
| 8 | + |
| 9 | +``` csharp |
| 10 | +private void textBox1_TextChanged(object sender, System.EventArgs e) |
| 11 | +{ |
| 12 | + this.sfDataGrid.SearchController.Search(this.textBox1.Text); |
| 13 | +} |
| 14 | + |
| 15 | +private void nextButton_Click(object sender, System.EventArgs e) |
| 16 | +{ |
| 17 | + this.sfDataGrid.SearchController.FindNext(this.textBox1.Text); |
| 18 | + SetSelectedItem(); |
| 19 | +} |
| 20 | + |
| 21 | +private void previousButton_Click(object sender, System.EventArgs e) |
| 22 | +{ |
| 23 | + this.sfDataGrid.SearchController.FindPrevious(this.textBox1.Text); |
| 24 | + SetSelectedItem(); |
| 25 | +} |
| 26 | + |
| 27 | +private void SetSelectedItem() |
| 28 | +{ |
| 29 | + var rowIndex = this.sfDataGrid.SearchController.CurrentRowColumnIndex.RowIndex; |
| 30 | + var recordIndex = this.sfDataGrid.TableControl.ResolveToRecordIndex(rowIndex); |
| 31 | + this.sfDataGrid.SelectedIndex = recordIndex; |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +### VB |
| 36 | + |
| 37 | +``` vb |
| 38 | +Private Sub textBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles textBox1.TextChanged |
| 39 | + Me.sfDataGrid.SearchController.Search(Me.textBox1.Text) |
| 40 | +End Sub |
| 41 | + |
| 42 | +Private Sub nextButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click |
| 43 | + Me.sfDataGrid.SearchController.FindNext(Me.textBox1.Text) |
| 44 | + SetSelectedItem() |
| 45 | +End Sub |
| 46 | + |
| 47 | +Private Sub previousButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button2.Click |
| 48 | + Me.sfDataGrid.SearchController.FindPrevious(Me.textBox1.Text) |
| 49 | + SetSelectedItem() |
| 50 | +End Sub |
| 51 | + |
| 52 | +Private Sub SetSelectedItem() |
| 53 | + Dim rowIndex = Me.sfDataGrid.SearchController.CurrentRowColumnIndex.RowIndex |
| 54 | + Dim recordIndex = Me.sfDataGrid.TableControl.ResolveToRecordIndex(rowIndex) |
| 55 | + Me.sfDataGrid.SelectedIndex = recordIndex |
| 56 | +End Sub |
| 57 | +``` |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +Take a moment to peruse the [WinForms DataGrid - Search](https://help.syncfusion.com/windowsforms/datagrid/search) documentation, where you can find about DataGrid with code examples. |
0 commit comments