简体   繁体   English

仅显示Gridview中的选定行

[英]Show only selected rows in Gridview

I have DataTable object and am binding it to a gridview in C#. 我有DataTable对象,并将其绑定到C#中的gridview。

I have 3 columns in the datatable, say "Flag", "Name", and "Value". 我在数据表中有3列,分别是“标记”,“名称”和“值”。

What I want to accomplish is that I want to only show the rows where "flag" fields are set to 0. 我要完成的是仅显示“标志”字段设置为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. 而且,我只想在gridview上显示“ tom”和“ 100”。

Is there any way I could do this without creating a new datatable? 有什么方法可以在不创建新数据表的情况下做到这一点?

Thanks. 谢谢。

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(...); 可能您可以使用相同的数据表,例如:table = table.Select(...);

try creating a DataView for your DataTable and send it to your GridView instead of the DataTable. 尝试为您的DataTable创建一个DataView并将其发送到GridView而不是DataTable。 see http://msdn.microsoft.com/en-us/library/system.data.dataview.aspx 请参阅http://msdn.microsoft.com/en-us/library/system.data.dataview.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM