locked
Tip: Add a Serial No. Column to your GridView RRS feed

  • Question

  • User1429119366 posted

    Allow me to share this simple yet useful tip with you all:

    protected int i = 1;

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

    if (e.Row.RowType == DataControlRowType.DataRow)

    {

    e.Row.Cells[0].Text = Convert.ToString(GridView1.PageIndex * GridView1.PageSize + i);

    i++;

    }

     

    }

     

    <asp:TemplateField HeaderText="Sr. No.">

    <ItemTemplate>

    <asp:Label ID="Label1" runat="server"></asp:Label>

    </ItemTemplate>

    </asp:TemplateField>

     

    Hope this helps someone.

    Fox 

     

     

     

    Saturday, November 4, 2006 3:57 AM

All replies

  • User-328271740 posted

    Great tip. You saved my Day [:D]

    Thursday, May 29, 2008 5:22 AM
  • User-519136805 posted

     

    Here is the Code to Generate Serial No in Gridview :

    <asp:GridView  runat=”server” id=”gvEmp”>
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <%#Container.DataItemIndex+1 %>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>



    </asp:GridView>

    Thursday, March 5, 2009 5:08 AM
  • User758091673 posted

    Hi sandy060583,

     

    Good tips thanks very much.

     

    regards, 

    Tuesday, July 20, 2010 2:07 AM