简体   繁体   中英

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

I am using the commands in the Gridview's RowDataBound Event and starting from 0 the gridview has 12 columns

Take into account that a GridView has some rows that are not data (pager, footer, etc).

I'd say you should have something like this so you only apply hiding logic to DataRow elements.

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 .

If you have autogeneratecolumns = true for your Gridview you may need to put the code into the RowCreated event instead of the RowDataBound event.

Here is a similar answer: How To Hide Columns with auto-generated columns

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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