views:

106

answers:

2

I have a very simple GridView on one of my pages with the following markup on my .aspx page:

<asp:GridView ID="gvNews" runat="server" AutoGenerateColumns="false" AllowPaging="true"
            AllowSorting="true" DataKeyNames="NewsID,VersionStamp" OnPageIndexChanging="gvNews_PageIndexChanging"
            OnRowCreated="gvNews_RowCreated">
            <Columns>
                <asp:BoundField HeaderText="News Title" DataField="NewsTitle"
                    SortExpression="NewsTitle" ReadOnly="true" />
                <asp:BoundField HeaderText="News Content" DataField="NewsContent"
                    SortExpression="NewsContent" ReadOnly="true" />
                <asp:BoundField HeaderText="Posted Date" DataField="InsertedDate"
                    SortExpression="InsertedDate" ReadOnly="True" />
                <asp:BoundField HeaderText="InsertedBy" DataField="InsertedBy" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="lbEdit" runat="server" Text="Edit" CommandName="Select" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Below is the code on my .cs page:

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadGrid();
            }
        }

        private void LoadGrid()
        {
            gvNews.DataSource = GetNews();
            gvNews.DataBind();
        }


        protected void gvNews_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

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

On the RowCreated event I am trying to hide the InsertedBy column in the gridview. This code works fine when AllowPaging is set to flase. But when the AllowPaging is set to true I get the following error in the RowCreated event handler:

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

What could be the reasons for this behavior?

A: 

From what you have posted your hard coded value of 3 in the RowCreated event seems like the problem. Enable tracing on the page and see what you get. BTW the pager next->prev links also cause postback and in PageLoad u are only loading grid if its not a postback which it is when u try to go for next page and the row created is fired.

Perpetualcoder
A: 

You need to write your code like this:

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

With a GridView there are different types of rows that might get created and they will have different numbers of cells, but the RowCreated event will fire for all rows, so you need to limit your logic to only data rows in this case.

Zach Parrish
Thanks, that worked perfectly. We also need to check the header row as well so that both the header and the datarows are invisible: if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header) { e.Row.Cells[3].Visible = false; }
Kumar