2X faster development
The ultimate WPF UI toolkit to boost your development speed.
You can apply the searching operation for one column by inheriting SearchHelper class in WPF DataGrid (SfDataGrid) and overriding the SearchCell and FilterRecords methods. In the sample, the searching operation has been performed for EmployeeId column based on the text entered in the text box and when the text box lost its focus or pressed the Enter key in the text box. Refer to the following code snippets to apply the search filter for one column in SfDataGrid. XAML <Grid> <Grid.RowDefinitions> <RowDefinition Height="50"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <WrapPanel Grid.Row="0"> <StackPanel Width="50" /> <TextBox x:Name="TextBox" Height="30" Width="100" /> <StackPanel Width="10" /> </WrapPanel> <syncfusion:SfDataGrid x:Name="dataGrid" Grid.Row="1" AutoGenerateColumns="True" AllowFiltering="True" AllowResizingColumns="True" EnableDataVirtualization="True" ShowBusyIndicator="True" AllowEditing="True" ItemsSource="{Binding Path=ItemSource}"/> </Grid> SerachHelperExt.cs public class SearchHelperExt : SearchHelper { public SearchHelperExt(SfDataGrid datagrid) : base(datagrid) { } protected override bool SearchCell(DataColumnBase column, object record, bool ApplySearchHighlightBrush) { if (column == null) return false; if (column.GridColumn.MappingName == "EmployeeId") { return base.SearchCell(column, record, ApplySearchHighlightBrush); } else return false; } protected override bool FilterRecords(object dataRow) { if (string.IsNullOrEmpty(SearchText)) return true; if (this.Provider == null) Provider = this.DataGrid.View.GetPropertyAccessProvider(); if (MatchSearchTextOpimized("EmployeeId", dataRow)) return true; return false; } protected virtual bool MatchSearchTextOpimized(string mappingName, object record) { if (this.DataGrid.View == null || string.IsNullOrEmpty(SearchText)) return false; var cellvalue = Provider.GetFormattedValue(record, mappingName); if (this.SearchType == SearchType.Contains) { if (!AllowCaseSensitiveSearch) return cellvalue != null && cellvalue.ToString().ToLower().Contains(SearchText.ToString().ToLower()); else return cellvalue != null && cellvalue.ToString().Contains(SearchText.ToString()); } else if (this.SearchType == SearchType.StartsWith) { if (!AllowCaseSensitiveSearch) return cellvalue != null && cellvalue.ToString().ToLower().StartsWith(SearchText.ToString().ToLower()); else return cellvalue != null && cellvalue.ToString().StartsWith(SearchText.ToString()); } else { if (!AllowCaseSensitiveSearch) return cellvalue != null && cellvalue.ToString().ToLower().EndsWith(SearchText.ToString().ToLower()); else return cellvalue != null && cellvalue.ToString().EndsWith(SearchText.ToString()); } } } MainWindow.xaml.cs public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.dataGrid.SearchHelper = new SearchHelperExt(this.dataGrid); this.TextBox.LostFocus += TextBox_LostFocus; this.TextBox.PreviewKeyDown += TextBox_PreviewKeyDown; } private void TextBox_LostFocus(object sender, RoutedEventArgs e) { PerformSearch(); } private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) PerformSearch(); } private void PerformSearch() { if (this.dataGrid.SearchHelper.SearchText.Equals(this.TextBox.Text)) return; var text = TextBox.Text; this.dataGrid.SearchHelper.AllowCaseSensitiveSearch = true; this.dataGrid.SearchHelper.SearchType = SearchType.StartsWith; this.dataGrid.SearchHelper.AllowFiltering = true; this.dataGrid.SearchHelper.Search(text); } } |
2X faster development
The ultimate WPF UI toolkit to boost your development speed.
This page will automatically be redirected to the sign-in page in 10 seconds.
This solution doesn't work for FindNext() and FindPrevious() methods.
Hi EloIT,
We have analyzed your query to perform FindNext and FindPrevious operation using search in one column. You can achieve this by overriding MatchSearchText in SearchHelperExt class as like below code snippet.
Please find sample for the same from the below link
Sample Link: http://www.syncfusion.com/downloads/support/directtrac/231305/ze/SearchingDemo133402893
Please let us know, if you need any further assistance on this.
Regards, Deivaselvan