BoldSignA modern eSignature application with affordable pricing. Sign up today for unlimited document usage!
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 ?
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.
thanks for the help, the code runs as i want.. have a nice day. :)
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 |
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.
thanks again for the help.. its working as i expected..