Click here to Skip to main content
15,879,490 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 7: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.

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[^]
 
Share this answer
 

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