在C#中的DataGridview中应用数值过滤器 [英] Apply Numerical Filters in DataGridview in C#

查看:113
本文介绍了在C#中的DataGridview中应用数值过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据网格视图,在一个列中包含数值,我试图应用过滤器,如show greater等等,但是我无法解决问题。



屏幕:





代码:



调用类方法并将值分配给datagridview

  dataGridViewKeywords = filter。过滤器(dataGridViewKeywords,mainValue,true); 

类文件:

  class FilterDataGridView 
{
private DataGridView modifiedDGV = new DataGridView();
私有字符串关键字;
private int competionScore;
DataTable dt = new DataTable();


public DataGridView Filter(DataGridView dgv,int value,bool is above)
{
modifiedDGV.Columns.Add(keyword,Keyword);
modifiedDGV.Columns.Add(比赛,平均比赛得分);
modifiedDGV.Columns.Add(moreinfo,更多信息);

如果(以上)
{
for(int row = 0; row< dgv.Rows.Count; row ++)
{
competionScore = Convert.ToInt32(dgv.Rows [row] .Cells [1] .Value);
keyword = dgv.Rows [row] .Cells [0] .Value.ToString();

if(competionScore> value)
modifiedDGV.Rows.Add(keyword,competionScore,View);
}
}

return modifiedDGV;
}


解决方案

最干净的解决方案是工作使用 BindingSource



您可以在 BindingSource -class,并将 DataGridView.DataSource 设置为此 BindingSource



BindingSource 本身或多或少是一个更好的List,还有一个 Filter -property可以让您轻松应用这些条件。这样你就不需要弄乱所有这些项目,甚至创建第二个 DataGridView


I have data gridview and in one colum contain numeric values and i am trying to apply filters like show greater and etc but I am not able to come with solution.

screen:

code:

calling the class method and assigning the value to datagridview

dataGridViewKeywords = filter.Filter(dataGridViewKeywords, mainValue, true);

Class File:

class FilterDataGridView
{
    private DataGridView modifiedDGV = new DataGridView();
    private string keyword;
    private int competionScore;
    DataTable dt = new DataTable();


    public DataGridView Filter (DataGridView dgv, int value, bool isabove)
    {
        modifiedDGV.Columns.Add("keyword", "Keyword") ;
        modifiedDGV.Columns.Add("competition", "Avg. Competition Score");
        modifiedDGV.Columns.Add("moreinfo", "More Info");

        if (isabove)
        {
            for (int row = 0; row < dgv.Rows.Count; row++)
            {
                competionScore = Convert.ToInt32(dgv.Rows[row].Cells[1].Value);
                keyword = dgv.Rows[row].Cells[0].Value.ToString();

                if (competionScore > value)
                    modifiedDGV.Rows.Add(keyword, competionScore, "View");
            }
        }

        return modifiedDGV;
    }

解决方案

The cleanest solution is to work with BindingSource.

You provide your data within a BindingSource-class and set the DataGridView.DataSource to this BindingSource.

BindingSource itself is more or less a better List and also has a Filter-property which lets you apply the criteria very easy. This way you don't have to mess around with all these items and even create a second DataGridView.

这篇关于在C#中的DataGridview中应用数值过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆