SfDataGrid - problem with filtering when searching

Hi

I set :

this.dataGrid.SearchHelper.AllowFiltering = true;
this.dataGrid.SearchHelper.Search(TextBox.Text);

as described in documentation. but still the records after search are not filtered (matching records are yellow highlighted,but records that do not match the searched pattern do not disappear) - is there any other setting which must be set to get  it working   ?  Filtering using UI filtering working ok.

In attachment demo project. It uses sqlconnection defined in mdanych.cs and query in SlownikOF.cs file in  line 23

Attachment: Demo_5b01de83.zip

3 Replies

JG Jai Ganesh S Syncfusion Team March 12, 2018 03:41 AM UTC

 
On analyzing on your provided sample application, you have used DataTable collection and while using DataTable collection the filtering will not work while searching. However, you can resolve this problem for converting the DataTable collection to dynamic collection. We have tried to run your sample but we got the below SQL collection error. 
 
 
 
 
We have prepared a simple sample to convert the DataTable collection as dynamic collection and make to work for the search filtering, 
 
dataTableCollection = GetGridData(); 
             
var dynamicCollection = new ObservableCollection<dynamic>(); 
foreach (DataRow row in dataTableCollection.Rows) 
{ 
    dynamic dyn = new ExpandoObject(); 
    dynamicCollection.Add(dyn); 
    foreach (DataColumn column in dataTableCollection.Columns) 
    { 
        var dic = (IDictionary<string, object>)dyn; 
        dic[column.ColumnName] = row[column]; 
    } 
} 
 
DynamicOrders = dynamicCollection; 
 
 
Could you please apply these code changes in your application and let us know whether the filtering will work while search? 
 
Regards, 
Jai Ganesh S


BK Bartosz Kaczuba March 12, 2018 10:08 AM UTC

Hi.

Now everything working fine,thank you :)

I have one more question about searching only selected column ? How to catch event when user click on the column header and next search only in  this column ? What is the easiest way to transfer selected ("clicked") column into SearchHelperExt ?

I want click on column header, enter searched text into textbox  and get grid filtered by this column and searched text.

I have for example  grid with columns : first name,last name,father name, mother name. Now when I search for "Stephan" it return records where first name is Stephan or  father name is Stephan,but I want get only records with first name=Stephan and ignore records with Stephan   in father name  column  


JG Jai Ganesh S Syncfusion Team March 15, 2018 10:28 AM UTC

You can get the column when clicking the column header in visualContainer_MouseLeftButtonUp event and perform search for this selected column in custom SearchHelper class. 
private void visualContainer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
{ 
    if (e.ClickCount == 1) 
    { 
        //GetVisualContainer  
        var visualContainer = this.AssociatedObject.sfGrid.GetVisualContainer(); 
        var rowcolumnindex = visualContainer.PointToCellRowColumnIndex(e.GetPosition(visualContainer)); 
        var columnindex = this.AssociatedObject.sfGrid.ResolveToGridVisibleColumnIndex(rowcolumnindex.ColumnIndex); 
        if (columnindex < 0) 
            return; 
        //Return if it is not HeaderRow 
        if (this.AssociatedObject.sfGrid.GetHeaderIndex() != rowcolumnindex.RowIndex) 
            return; 
                 
        var column = this.AssociatedObject.sfGrid.Columns[columnindex]; 
 
        var dataContext = this.AssociatedObject.DataContext as ViewModel; 
 
        dataContext.MappingName = column.MappingName; 
    } 
} 
 
public class SearchHelperExt : SearchHelper 
    { 
        public SearchHelperExt(SfDataGrid datagrid) 
            : base(datagrid) 
        { 
        } 
 
        protected override bool SearchCell(DataColumnBase column, object record, bool ApplySearchHighlightBrush) 
        { 
            var dataContext = this.DataGrid.DataContext as ViewModel; 
 
            // To perform the search operation based on clicking the column header 
            if (column.GridColumn.MappingName == dataContext.MappingName) 
                return base.SearchCell(column, record, ApplySearchHighlightBrush); 
            return false; 
        } 
 
        protected override bool FilterRecords(object dataRow) 
        { 
            if (string.IsNullOrEmpty(SearchText)) 
                return true; 
 
            var dataContext = this.DataGrid.DataContext as ViewModel; 
 
            if (this.Provider == null) 
                Provider = this.DataGrid.View.GetPropertyAccessProvider(); 
 
            int colindex = this.DataGrid.ResolveToGridVisibleColumnIndex(this.DataGrid.GetFirstColumnIndex()); 
            for (int i = colindex; i < this.DataGrid.Columns.Count; i++) 
            { 
                var column = this.DataGrid.Columns[i]; 
 
                // To perform the Filtering operation based on the clicking the column header 
                if (column != null && (column.MappingName == dataContext.MappingName) && column.ActualWidth != 0.0 && !column.IsHidden) 
                { 
                    if (MatchSearchText(column, dataRow)) 
                        return true; 
                } 
            } 
            return false; 
        } 
    } 
 
UG Link: 
Also, please find the KB link to search the column based on the selected item in SfDataGrid. 
Regards, 
Jai Ganesh S 


Loader.
Up arrow icon