0
votes

I have DataTable object and am binding it to a gridview in C#.

I have 3 columns in the datatable, say "Flag", "Name", and "Value".

What I want to accomplish is that I want to only show the rows where "flag" fields are set to 0.

So say if I have two rows in the table,

Flag   Name  Value
------------------    
0      tom    100
1      Jane   200

And, I only want to show "tom" and "100" on the gridview.

Is there any way I could do this without creating a new datatable?

Thanks.

2
What are you binding the datasource to? SqlDataSource? LinqDataSource?CodingGorilla
How do you populate the DataTable? there is probably a way to filter what you populate it with before you get the data.Remus
At runtime, I created the table by creating datarow and add it to the datatable. Reason why I want to do this is that I want to keep the original copy (DataTabe) around between two asp.net pages and display the row(s) based on the values in the flag.Tony

2 Answers

0
votes

Here is an example :

DataTable table = DataSet1.Tables["Orders"];
// Presuming the DataTable has a column named Date.
string expression;
expression = "Date > #1/1/00#";
DataRow[] foundRows;

// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression);

// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++)
{
    Console.WriteLine(foundRows[i][0]);
}

You can see example Here

Probably you can take the same Datatable like : table = table.Select(...);

0
votes

try creating a DataView for your DataTable and send it to your GridView instead of the DataTable. see http://msdn.microsoft.com/en-us/library/system.data.dataview.aspx