Hi,

I am using below code to hide a column in Gridview. Gridview have one template field(Checkbox) and multiple coulmns.And Auto generated field propert is true,remember there is no bound field.But it display an error.

My code::

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

e.Row.Cells[4].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

Recommended Answers

All 5 Replies

It means that the index '4' in the collection e.Row.Cells is outside the bounds of that collection. So it throws the error.

The first column starts with the index 0. Therefore you need to specify the index as 3 if you want to hide the 4th displayed column of the GridView.

Change your code as below

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)        
        {            
            e.Row.Controls[3].Visible = false;            
        }
    }

Instead of handling RowDataBound, you can also hide a auto generated column after binding your GridView. See this code.

GridView1.DataSource = yourDataTable;
        GridView1.DataBind();
        GridView1.HeaderRow.Cells[3].Visible = false;
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            gvr.Cells[3].Visible = false;
        }

It means that the index '4' in the collection e.Row.Cells is outside the bounds of that collection. So it throws the error.

The first column starts with the index 0. Therefore you need to specify the index as 3 if you want to hide the 4th displayed column of the GridView.

Change your code as below

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)        
        {            
            e.Row.Controls[3].Visible = false;            
        }
    }

Instead of handling RowDataBound, you can also hide a auto generated column after binding your GridView. See this code.

GridView1.DataSource = yourDataTable;
        GridView1.DataBind();
        GridView1.HeaderRow.Cells[3].Visible = false;
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            gvr.Cells[3].Visible = false;
        }

Thanks !

Hi Pankaj,

Mark this thread as solved if your question is answered.

Hello Friend,

For hide column in gridview

protected void Gridview1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Index of column to be hide
e.Row.Cells[0].Visible = false;

}
if(e.Row.RowType == DataControlRowType.Header)
{
// Index of column to be hide
e.Row.Cells[0].Visible = false;
}
}

I hope this will help you.

Thanks & Regards,
Sourabh S. Malani

this help me a lot! thank you

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.