Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

winforms - Apply Numerical Filters in DataGridview in C#

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:

enter image description here

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;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...