0

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

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

Skullomania
  • 2,225
  • 2
  • 29
  • 65
  • Have you set a breakpoint to examine the data you are binding the gridview to at runtime? Also, is your gridview set to autogeneratecolumns = true? – nycdan Oct 29 '13 at 15:42

2 Answers2

1

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.

Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
  • 1
    I believe if you are hiding columns, you don't need to do it in the DataRow section. Not sure but I think the column count should be consistent in the header and data. – nycdan Oct 29 '13 at 15:41
1

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

Community
  • 1
  • 1
nycdan
  • 2,819
  • 2
  • 21
  • 33