Click here to Skip to main content
15,943,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello guys,
How to hide or remove following row if cell value meet the condition. Exaple

Column1 Column2 Column3
A. 1 H
H. 7 g
L. 9 f
B. 3 a


If Column2 cell value = 7 then stop on that row..the following row will hide or deleted..

I've been searching this for whole day but no luck...hope you can help me guys.

BTW. C# .Net Desktop Application

What I have tried:

Btw. DataGrid is bound in data table...
Posted
Updated 5-Sep-22 2:59am
Comments
Richard MacCutchan 4-Sep-22 3:30am    
You need to remove the row from the DataTable and then refresh the DataGrid.
Maciej Los 4-Sep-22 4:44am    
Sounds like an answer.
Richard MacCutchan 4-Sep-22 7:21am    
If you think a guess is an answer. :)

First of all - you can fetch into a datatable object only the data you want to get, by filtering a database in a query:
SQL
SELECT ...
FROM ...
WHERE Column2 <> 7;


Second - you can filter datatable object:
DataTable.Select Method (System.Data) | Microsoft Docs[^]

Third - filter data by Linq:
Filtering with DataView (LINQ to DataSet) - ADO.NET | Microsoft Docs[^]

There's probably few other ways to achieve that.
 
Share this answer
 
v2

I am not sure how you are binding your data table to the view, but here are a couple of suggestions that may be useful. If it is permissable to exclude the row where Column2 is equal to 7, you can use TakeWhile.

C#
IEnumerable<DataRow> query= dataTable.AsEnumerable().
                           TakeWhile(r => long.Parse(r["Column2"].ToString()) != 7); 
var myDataTable = query.CopyToDataTable();


If you need to include the row where Coulumn2 is equal to a given value, you could wite a method similar to this.
C#
public  IEnumerable<DataRow> GetUpTo(Func<long,bool>isFirstOf)
    {
       foreach(var row in dataTable.AsEnumerable())
       {
          yield return row;
          if (isFirstOf(long.Parse(row["Column2"].ToString()))) break;
       }

    }

You can call it like this
C#
var redactedDataTable = GetUpTo(x=>x==7).CopyToDataTable();

 
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