Click here to Skip to main content
15,112,198 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have found a solution but I can't figure out the final code. Anybody please can help me out? I want to filter my dataridView by TextBox value after clicking on the button without database. So a very simple Filter.

Thank you!

What I have tried:

C#
private void FilterButton_Click(object sender, EventArgs e)
{
    DataTable dt = ((DataTable)DataGridView2.DataSource);
    foreach (DataGridViewRow row in DataGridView2.Rows)
    {
        // Test if the first column of the current row equals
        // the value in the text box
        if ((String)row.Cells["Month"].Value == birthdayMonth.Text)
        {
            // we have a match
            row.Selected = true;
        }
        else
        {
            row.Selected = false;

        }
    }
}
Posted
Updated 29-Jul-18 8:05am
v2
Comments
Richard MacCutchan 29-Jul-18 9:50am
   
It may be that row.Cells["Month"].Value and birthdayMonth.Text are different types, or even different values. But you have not told us, and we cannot guess.
Galarist_00 29-Jul-18 9:54am
   
I don't know what you mean.
Kenneth Haugland 29-Jul-18 10:08am
   
He asks if you print out row.Cells["Month"].Value what do you see? In general, it is better to say something like ToLower on both and use Contains in order to do filtering.
Galarist_00 29-Jul-18 10:13am
   
Nothing will change. Also I updated the link of my post.
Kenneth Haugland 29-Jul-18 10:18am
   
You actually didnt answer the question.
Mike V Baker 29-Jul-18 11:29am
   
They're asking if the types of data you're comparing are both 'text' type. I'm going to assume that both (String)row.Cells["Month"].Value and birthdayMonth.Text are text type items.
It appears that you've created a DataTable and set that as the DataSource for the grid. To filter the contents of the grid you want to take action on the list, so look for functions in DataTable supporting filter. I usually use a n-tier system and my data sources are List<mytype> so I'm not sure which function to call (or I would post it as a solution). You might also need to call the grid BindingSource.ResetBindings

1 solution

You should go back and review the "other" solution.

You picked the "easiest" answer but also the one with "zero votes".

You should probably consider the one "down a page".

Quote:
DataView dv = ((DataTable)DataGridValue.DataSource).DefaultView;
dv.RowFilter = "ColumnName < TB1 AND ColumName > TB2"

Afterwards bind the DataView to your gridView


FROM

c# - Filtering data row value in column DataGridView with value inside two textbox - Stack Overflow[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900