繁体   English   中英

如何动态隐藏多个gridview列?

[英]How do I make several gridview columns invisible dynamically?

我在用

e.Row.Cells[0].Visible = false;

使单个列不可见。 它工作,但当我尝试添加另一个像这样:

e.Row.Cells[0].Visible = false; 
e.Row.Cells[1].Visible = false; //i tried listing all and still got the out of range error 

我得到错误Specified argument was out of the range of valid values. Parameter name: index Specified argument was out of the range of valid values. Parameter name: index

我正在使用Gridview的RowDataBound事件中的命令,从0开始,gridview有12列

考虑到GridView有一些不是数据的行(寻呼机,页脚等)。

我说你应该有这样的事情,所以你只能躲在逻辑应用到DataRow元素。

if (e.Row.RowType == DataControlRowType.DataRow)
{
    e.Row.Cells[0].Visible = false; 
    e.Row.Cells[1].Visible = false;
}

要查看所有行类型,请查看此MSDN文章

如果Gridview的RowCreated = true,则可能需要将代码放入RowCreated事件而不是RowDataBound事件。

以下是类似的答案: 如何使用自动生成的列隐藏列

暂无
暂无

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

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