简体   繁体   English

如何动态隐藏多个gridview列?

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

I am using 我在用

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

to make a single column invisible. 使单个列不可见。 It works but when I try to add another like so: 它工作,但当我尝试添加另一个像这样:

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

I get the 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 Specified argument was out of the range of valid values. Parameter name: index

I am using the commands in the Gridview's RowDataBound Event and starting from 0 the gridview has 12 columns 我正在使用Gridview的RowDataBound事件中的命令,从0开始,gridview有12列

Take into account that a GridView has some rows that are not data (pager, footer, etc). 考虑到GridView有一些不是数据的行(寻呼机,页脚等)。

I'd say you should have something like this so you only apply hiding logic to DataRow elements. 我说你应该有这样的事情,所以你只能躲在逻辑应用到DataRow元素。

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

To see all row types check this MSDN article . 要查看所有行类型,请查看此MSDN文章

If you have autogeneratecolumns = true for your Gridview you may need to put the code into the RowCreated event instead of the RowDataBound event. 如果Gridview的RowCreated = true,则可能需要将代码放入RowCreated事件而不是RowDataBound事件。

Here is a similar answer: How To Hide Columns with auto-generated columns 以下是类似的答案: 如何使用自动生成的列隐藏列

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

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