【问题标题】:Show only selected rows in Gridview在 Gridview 中仅显示选定的行
【发布时间】:2011-05-07 10:37:18
【问题描述】:

我有 DataTable 对象,并将其绑定到 C# 中的 gridview。

我在数据表中有 3 列,分别是“标志”、“名称”和“值”。

我想要完成的是我只想显示“标志”字段设置为 0 的行。

假设我的表格中有两行,

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

而且,我只想在 gridview 上显示“tom”和“100”。

有什么方法可以在不创建新数据表的情况下做到这一点?

谢谢。

【问题讨论】:

  • 你将数据源绑定到什么? SqlDataSource? LinqDataSource?
  • 如何填充数据表?可能有一种方法可以在获取数据之前过滤您填充的内容。
  • 在运行时,我通过创建数据行来创建表并将其添加到数据表中。我想这样做的原因是我想将原始副本(DataTabe)保留在两个 asp.net 页面之间,并根据标志中的值显示行。

标签: c#


【解决方案1】:

尝试为您的 DataTable 创建一个 DataView 并将其发送到您的 GridView 而不是 DataTable。见http://msdn.microsoft.com/en-us/library/system.data.dataview.aspx

【讨论】:

    【解决方案2】:

    这是一个例子:

    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]);
    }
    

    你可以看例子Here

    也许您可以采用相同的数据表,例如:table = table.Select(...);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      相关资源
      最近更新 更多