0

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

4
Contributors
5
Replies
9
Views
8 Years
Discussion Span
Last Post by mitchikoy
0

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;
        }

Edited by Ramesh S: n/a

0

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 !

0

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

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.