Search This Blog

Thursday, September 23, 2010

create a column that will autogenerate the serial number of each row in GridView


I want to add auto generated Serial number as a column in grid view which is not from database.
So, I tried like this, and its working at all.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblSerial = (Label)e.Row.FindControl("lblSerial");
            lblSerial.Text = ((GridView1.PageIndex * GridView1.PageSize) + e.Row.RowIndex +                 1).ToString();
        }
    }


Or By Adding a Template column like the following we can do this,


<asp:TemplateField>
    <ItemTemplate>
        <%# Container.DataItemIndex + 1 %>
    </ItemTemplate>
</asp:TemplateField>

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.