Hi,

I am using below code to hide a column in Gridview. But it display an error.

My code::

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[7].Visible =false;//error generated on this line

    }

Error::
Specified argument was out of the range of valid values Parameter name: index

Please help me.

Thanks !

Pankaj

Hi Pankaj,
I had a doubt in your question. Why dont u directly bind the grid with out that field (column). Y do u want that as a hidden entry in gridview.

Best Regards,
Phani Chavali

Hi Pankaj,
I had a doubt in your question. Why dont u directly bind the grid with out that field (column). Y do u want that as a hidden entry in gridview.

Best Regards,
Phani Chavali

Hi,
i want as a hidden entry in gridview, because this information is internal to the Project and has nothing to do with User.

help me..
Thanks !
Pankaj

Hi Pankaj
I have written the same code in my row databound to hide that column and succesfully its hiding the column in the datagrid.


Here is the code.. you can check...

protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       e.Row.Cells[6].Visible = false;
    }

in my code i have 7 columns... i just want to hide 7th column thats y i used above line.

I think in ur code you just check the number of columns first...

for example if your db have 5 columns and you want to hide 3rd column then you need to set as

e.Row.Cells[2].Visible=false;
//in row databound


Try and check with your number of columns...


Best Regards,
Phani Chavali.

The index of the first column starts with 0 and last index is 'Total columns' -1. That is if your GridView has 7 columns then the last index will be 6.

Also you can hide a column in a GridView by the following method

GridView1.Columns[6].Visible = false;

EDIT TO THE FOLLOWING:

Sorry, I thought by the thread name you were having trouble accessing the value of a hidden column. The code string you are looking for is:

e.Columns[7].Visible = false;

I think someone above had already posted that. I tested it in a gridview I had and it did the job.


Hi,

I've tried accessing columns in a gridview that are hidden. The solutions I found may not be the most elegant. I'll keep an eye out on this thread and hopefully someone has a more elegant solution.
The first method I found lets you keep one column hidden. You set the column you want hidden as the datakey. I'm not sure the vb equivalent of the following code, but I use this to grab the datakey from the selected row:

var VariableName = GridName.DataKeys[row.RowIndex].Value.ToString();

Obviously variablename and gridname refer to the variable you want to call the selected item, and the grid you are selecting.

The other, even less elegant way I've done it was to put both the font and grid background to the same color as the page background (so it's effectively invisible) and shrink the font to something like .01pt so it doesn't take much space.
Like I said, not the best solution, but it works. Hopefully someone has a better one.

hi,
This line (e.Columns[7].Visible = false;) working for only bound fields not for auto generated fields.

Thanks!
Pankaj

This question has already been answered. Start a new discussion instead.