winforms 如何在sfDatagrid中根据列之间值的比较来筛选数据?

mwngjboj  于 2022-12-04  发布在  其他
关注(0)|答案(1)|浏览(125)

这里我使用syncfusion框架,我使用这个框架的时间还不长。我的问题的意思是:
我有3列。“Product_Name”,“Cost_Price”,“Sales_Price”和其中的50行数据。现在,我想基于列“Sales_Price”中的值小于或等于列“Cost_Price”中的值,通过sfButton1上的click事件以编程方式实现数据过滤器。我只想显示Sales_Price有问题的数据,即当Sales_Price值〈= Cost_Price值时。
在前面的实验中,我尝试使用telerik,这与我的要求一致:

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);
    }

但是我使用的telerik只是一个试用版,很快就会过期,所以我使用syncfusion,它可以完全控制社区许可证。
这是我从同步文档中得到的代码:但它只根据〈0的值过滤“Stock”列中的数据:

B_1_DGV_Stock.Columns["Stock"].FilterPredicates.Add(new FilterPredicate() { FilterType = FilterType.LessThan, FilterValue = "0" });
of1yzvn4

of1yzvn41#

通过使用下面所示SfDataGrid视图筛选概念,可以实现此要求:

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();
}

有关编程筛选的详细信息,请参阅以下用户指南文档链接,
UG链接:https://help.syncfusion.com/windowsforms/datagrid/filtering#programmatic-filtering
同样,DataTable也不支持此功能。如果您也需要使用Datatable集合实现相同的功能,则可以使用下面所示的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; }
}

过滤概念:

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();
}

相关问题