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

Dani AI

Generated

The "index out of range" error means the row you're examining doesn't have that many cells (header/footer rows, AutoGenerateColumns differences, or extra command/select columns can change counts). is seeing that because the code assumes a fixed cell index; pointed out indexing basics and already suggested rethinking binding. Two robust patterns are preferred for keeping data hidden from users while still accessible server-side: DataKeys (best for server-only values) or a HiddenField inside a TemplateField (when you need the value per-row on postback).

Use DataKeyNames so the grid stores the value independently of visible cells (set in markup). Then read the DataKey for the row you need (RowCommand, SelectedIndexChanged, etc.):

<asp:GridView ID="GridView1" runat="server" DataKeyNames="Id,SecretValue" ...>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int rowIndex = Convert.ToInt32(e.CommandArgument);
    string secret = GridView1.DataKeys[rowIndex].Values["SecretValue"].ToString();
}

If you prefer a TemplateField, put a HiddenField in the ItemTemplate and retrieve it with FindControl from the row:

<ItemTemplate>
  <asp:HiddenField ID="hidSecret" runat="server" Value='<%# Eval("SecretValue") %>' />
  <%# Eval("VisibleCol") %>
</ItemTemplate>
GridViewRow r = GridView1.Rows[rowIndex];
var hf = (HiddenField)r.FindControl("hidSecret");
string secret = hf.Value;

Troubleshooting tips:

  • Don’t assume a fixed cell index: check e.Row.RowType and e.Row.Cells.Count before touching Cells[...].
  • If AutoGenerateColumns is true, the Columns collection is not populated—hide columns in markup or use TemplateFields instead.
  • Hiding a cell with Visible=false removes it from the rendered table (affects indices); using CSS display:none keeps DOM positions but only hides visually.
  • Avoid placing truly sensitive secrets in client-side controls (HiddenField/ViewState can be inspected or tampered with); re-query on the server or use secure server-side storage for critical data.

These approaches keep the value hidden from users while avoiding index errors and brittle cell-index logic.

Recommended Answers

All 6 Replies

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.