Hi,

I have a gridview with autogenerated fields, 2 template fields, 1 command field. Now, I want to hide one of the columns in a gridview .

I have a code written in rowdatabound

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

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

    }

this code works well when I have not introduced allow paging=true.

but when I have a page indexing property my code for pageindexingchanging is:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

gridview1.pageindex=e.newpageindex;

 fillgrid();

     }

where fillgrid is a function which is simply binding data coming from data base in gridview.

when I add page indexing to my gridview I get an error:

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

on this line:

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

any help shall be appreciated.

thanks
shankbond

why not hide the column in fillGrid method?

if (yourCondition) {
   gridview1.Columns[2].Visible = false;
}

See if this works.

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.