【问题标题】:How to remove row from datagrid in asp.net如何从asp.net中的datagrid中删除行
【发布时间】:2014-05-22 14:40:34
【问题描述】:

我有一个 datagrid dgCompanies 声明如下:

// bind the data to the datagrid
dgCompanies.PageSize = pageSize;
dgCompanies.DataSource = rdr;

然后我需要检查这个datagrid里面的记录:

int j = 0;
foreach (DataGridItem item in dgCompanies.Items)
{
    HtmlGenericControl name = (HtmlGenericControl)item.Cells[j].FindControl("SpanTitle");   
    string drstring = name.InnerHtml.Trim();

    if (checkingfunction(drstring))
    {
       //do removing record from datagrid.
        I tried this:  item.Cells.Remove(item.Cells[j]); but the result still there, don't see anything removed!
    }

    j = j + 1;                    
 }

 dgCompanies.DataBind();

如果条件满足,我如何从数据网格dgCompanies 中删除该记录?

【问题讨论】:

  • 过滤数据源不是更简单更自然吗?
  • 我认为这并不容易,请在这里参考我的问题:stackoverflow.com/questions/23774851/…
  • 我试过了:item.Cells.Remove(item.Cells[j]);但结果仍然存在,没有看到任何删除!

标签: c# asp.net datagrid


【解决方案1】:

我同意@Andrei,最好不要返回该数据。否则,您将返回并搅动不必要的数据。但是,如果在您无法预先过滤数据的情况下,我想您可以在行中添加一个 css 类“donotshow”(请参阅​​How do I specify CSS classes for specific rows in a GridView?)。

然后使用jquery使用

$('.donotshow').hide();

同样,在这方面,您仍将所有 HTML 返回到浏览器,这会使您的页面大小超出所需。此外,如果您使用这种方法,可能会搞砸分页(例如,如果它说“第 1-10 行”,但隐藏了 5 行,那么这看起来很愚蠢)。

希望对你有帮助

【讨论】:

  • 如果条件满足,我如何“标记”代码隐藏中的行?那么如何隐藏呢?你能举个代码隐藏和 ascx 文件的例子吗?
  • 我试过这个:item.Cells[j].Visible = false;但我也没有看到任何变化。
  • 我还需要在 ascx 文件中进行更改吗?
猜你喜欢
  • 2016-02-17
  • 1970-01-01
  • 2010-12-03
  • 1970-01-01
  • 2016-08-20
  • 1970-01-01
  • 2015-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多