We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to filter SFDataGrid and Itemsource as DataTable on all columns

Dear, 

I have used datatable as itemsource in sfdatagrid. Now I want to filter records based on the search bar text entered but the filtering should consider all columns with like matching pattern for string as well as numeric columns. Please help me to reset filter also.


Source code for binding Itemsource to dataGrid

dataGrid.AutoGenerateColumns = true;

DataTable dt = new DataTable();

dt = JsonConvert.DeserializeObject<DataTable>(jsonResp["data"].ToString().Trim());

dataGrid.CanUseViewFilter = true;

DV = new DataView(dt);

dataGrid.ItemsSource = DV;


Code of TextChanged event of Searchvbar

private void Search_TextChanged(object sender, TextChangedEventArgs e)

{

            if (e.NewTextValue == null)

            {

                DV.RowFilter = null;

            }

            else

            {

                //this.viewModel.FilterText = e.NewTextValue;

                DV.RowFilter = "Unit = '" + e.NewTextValue + "'";

            }

}

here Unit is my dataGrid Column Name.


Thanking you.



1 Reply

LN Lakshmi Natarajan Syncfusion Team October 24, 2022 12:56 PM

Hi Amish,


The SfDataGrid does not support default filtering when binding DataTable as ItemsSource. You can also achieve your requirement by manually filtering the data table.


Please refer to the following code snippets for more reference,

private void search_TextChanged(object sender, TextChangedEventArgs e)

{

    var filterText = e.NewTextValue;

 

    if (string.IsNullOrEmpty(e.NewTextValue))

    {

        dataGrid.ItemsSource = DV;

    }

    else

    {

        var filterQuery = from order in viewModel.dt.AsEnumerable()

                                                    where order.Field<string>("Customer").ToLower().Contains(filterText.ToLower())

                                                    || order.Field<int>("OrderID").ToString().Contains(filterText.ToLower())

                                                    || order.Field<string>("ShipCity").ToLower().Contains(filterText.ToLower())

                                                    || order.Field<string>("CustomerID").ToLower().Contains(filterText.ToLower())

                    select order;

 

        DataView dv = new DataView(filterQuery.CopyToDataTable());

        dataGrid.ItemsSource = dv;

    }


We have attached the sample to filter the DataGrid rows by considering all columns in this update. Please check the sample and let us know if you need further assistance.


Regards,

Lakshmi Natarajan



Attachment: DataGridXamarin_837256fc.zip

Loader.
Live Chat Icon For mobile
Up arrow icon