here I use the syncfusion framework, and it hasn't been too long since I've used this framework. what I mean from my question is:
i have 3 columns. "Product_Name", "Cost_Price", "Sales_Price" and 50 rows of data in it. now i want to implement data filter programmatically with click event on sfButton1 based on value in column "Sales_Price" less than or equal to value in column "Cost_Price". and I only want to display data that has problem with Sales_Price, i.e. when Sales_Price Value <= Cost_Price value.
in the previous experiment I tried using telerik and this is in accordance with what I asked:
private void RAD_GridView_ShowData_CustomFiltering(object sender, GridViewCustomFilteringEventArgs e)
{
e.Visible = (int)e.Row.Cells["Sales_price"].Value <= (int)e.Row.Cells["Cost_price"].Value;
}
private void Btn_ShowFilter_Click(object sender, LinkLabelLinkClickedEventArgs e)
{
RAD_GridView_ShowData.EnableFiltering = true;
RAD_GridView_ShowData.EnableCustomFiltering = true;
RAD_GridView_ShowData.CustomFiltering = new GridViewCustomFilteringEventHandler(RAD_GridView_ShowData_CustomFiltering);
FilterDescriptor descriptor = new FilterDescriptor("Sales_price", FilterOperator.IsGreaterThanOrEqualTo, 0);
RAD_GridView_ShowData.FilterDescriptors.Add(descriptor);
}
but the telerik that I use is only a trial and will expire soon, so I use syncfusion which gives full control with the community license.
this code I got from the sync documentation: but it only filters data in "Stock" column based on value < 0 :
B_1_DGV_Stock.Columns["Stock"].FilterPredicates.Add(new FilterPredicate() { FilterType = FilterType.LessThan, FilterValue = "0" });
CodePudding user response:
This requirement will be achievable by using SfDataGrid’s view filtering concept 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();
}
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
Also, this is not supported for the DataTable. If you need to achieve the same with the Datatable collection too, it will be possible by using the 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();
}