Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to hide all rows in datagrid which do not match the text in Access.text label, How would I accomplish this?


foreach (DataGridViewRow row in productionDGV.Rows)
{
    if (row.Cells[4].Value.ToString() == Access.Text)
    {
        row.Visible = true;
    }
    else if (row.Cells[4].Value.ToString() != Access.Text)
    {
        row.Visible = false;
    }
}


What I have tried:

foreach (DataGridViewRow row in productionDGV.Rows)
 {
     if (row.Cells[4].Value.ToString() == Access.Text)
     {
         row.Visible = true;
     }
     else if (row.Cells[4].Value.ToString() != Access.Text)
     {
         row.Visible = false;
     }
Posted
Updated 5-May-18 19:49pm
Comments
Krunal Rohit 6-May-18 1:46am    
And you're output with this code is ?

KR
Member 13765884 6-May-18 4:11am    
It returns with a full table including the data which should be hidden

1 solution

In general you can set the row as invisible by setting the Visible property to true. However, you may need to suspend the binding while setting the property. Consider the following, replace the myDataTable with the actual object you use as the data source.

C#
BindingContext[myDataTable].SuspendBinding();
foreach (DataGridViewRow row in this.productionDGV.Rows) {
   if (row.Cells[4].Value.ToString() != Access.Text) {
      row.Visible = false;
   }
}
BindingContext[myDataTable].ResumeBinding();
 
Share this answer
 
Comments
Member 13765884 6-May-18 6:55am    
Thank you. I've written it as ...


BindingContext[productionBindingSource].SuspendBinding();
foreach (DataGridViewRow row in this.productionDGV.Rows)
{
if (row.Cells[4].Value.ToString() != Access.Text)
{
row.Visible = false;
}
}
BindingContext[productionBindingSource].ResumeBinding();


But now, The grid is coming up empty. as the debugger isn't coming up with an error- can you help please?
Wendelius 6-May-18 7:22am    
You're now comparing Cell 0 (= first column), is that the correct cell?
Member 13765884 6-May-18 7:25am    
No, it is not correct. I was trying to try different things to see if the error could be something else. It is still actually cell 4 that I'm trying to compare
Wendelius 6-May-18 11:22am    
So if you use Cells[4] which is the 5th column does it work as expected?
Member 13765884 6-May-18 11:48am    
no, it doesn't. I've tried all rows.

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