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 apply filter based on certain condition.

i have 3 columns and 50 rows of data. "Product_Name", "Cost_Price", "Sales_Price". now i want to implement data filter programmatically with click event on sfButton1 based on the value in column "Sales_Price" less than the value in column "Cost_Price". how to achieve that ?


5 Replies 1 reply marked as answer

DM Dhanasekar Mohanraj Syncfusion Team November 30, 2022 03:44 PM UTC

Hi RIDWAN,


You can achieve your requirement to apply the filter programmatically based on the condition will be achievable by using the ViewFiltering shown below,

public bool FilterRecords(object o)

{

    var item = o as OrderInfo;

    if (item != null)

    {

        if (item.UnitPrice <= item.Quantity)

            return true;

    }

    return false;

}

 

private void button1_Click(object sender, EventArgs e)

{

    sfDataGrid1.View.Filter = FilterRecords;

    sfDataGrid1.View.RefreshFilter();

}


Here we have prepared the sample based on your scenario, please have a look at this. For more information related to Programmatic Filtering, please refer to the below user guide documentation link,

UG Link: https://help.syncfusion.com/windowsforms/datagrid/filtering#programmatic-filtering


Regards,

Dhanasekar M.


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: SfDatagridDemo_d788ce2e.zip


RI RIDWAN replied to Dhanasekar Mohanraj December 1, 2022 03:53 AM UTC

thanks for the help, the code runs as i want.. have a nice day. :)



RI RIDWAN replied to Dhanasekar Mohanraj December 1, 2022 05:42 AM UTC

one more question, how is the filter also applied to the data table? as an example, I use this code to calculate the amount of data between 2 columns when "Sales_Price" <= "Cost_Price".

int Calculate = Dt.AsEnumerable().Where(x => x.Field("Sales_Price") < x.Field("Cost_Price")).Count();




DM Dhanasekar Mohanraj Syncfusion Team December 1, 2022 01:58 PM UTC

RIDWAN,

By default, our SfDataGrid’s View filters is not supported when the DataSource is DataTable. We have already documented this as a limitation as shown below,

However, you can achieve your requirement to filter the SfDataGrid’s records by comparing the two column values with the DataTable bounded grid by using ExpandoObject shown below,

ExpandoObject:

private DataTable dataTableCollection;

private ObservableCollection<dynamic> dynamicCollection;

public Form1()

{

    InitializeComponent();

    this.WindowState = FormWindowState.Maximized;

    //Gets the data for DataTable object.

    dataTableCollection = GetGridData();

 

    //Convert DataTable collection as Dyanamic collection.

    dynamicCollection = new ObservableCollection<dynamic>();

    foreach (System.Data.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;

    sfDataGrid1.AutoGenerateColumns = true;

    sfDataGrid1.DataSource = DynamicOrders;

}

 

 

private ObservableCollection<dynamic> _dynamicOrders;

 

/// <summary>

/// Gets or sets the dynamic orders.

/// </summary>

/// <value>The dynamic orders.</value>

public ObservableCollection<dynamic> DynamicOrders

{

    get

    {

        return _dynamicOrders;

    }

    set

    {

        _dynamicOrders = value;

    }

}

 

public DataTable DataTableCollection

{

    get { return dataTableCollection; }

    set { dataTableCollection = value; }

}


Filtering Concept:


public bool FilterRecords(object o)

{

    var emplyeeAge = ((ExpandoObject)o).FirstOrDefault(z => z.Key == "EmployeeAge").Value;

    var memmbersCount = ((ExpandoObject)o).FirstOrDefault(z => z.Key == "MemmbersCount").Value;

    if (emplyeeAge != null && memmbersCount != null)

    {

        if (double.Parse(emplyeeAge.ToString()) < (double.Parse(memmbersCount.ToString())))

            return true;

    }

    return false;

}

 

private void FilterClick(object sender, System.EventArgs e)

{        

    sfDataGrid1.View.Filter = FilterRecords;

    sfDataGrid1.View.RefreshFilter();

}


Here we have modified the attached sample as you requested. Please have a look at this.

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: SfDatagridDemo__258e1d5a.zip

Marked as answer

RI RIDWAN replied to Dhanasekar Mohanraj December 2, 2022 07:57 AM UTC

thanks again for the help.. its working as i expected..


Loader.
Live Chat Icon For mobile
Up arrow icon