How to Generate Serial Number in Gridview ASP

rochaksite_tutorial

In this tutorial we will see how  we can add Auto Generated serial number in Gridview. Before starting tutorial we assume you are aware about grid view in c # which use to display data in a grid system. Here are some simple steps for doing this.

Step 1 : Create a Gridview, Simple adding From Toolbox Menu.

Step 2 : Add this line inside the <Columns>——— </Columns> tag.

<asp:TemplateField HeaderText="S.No"><ItemStyle HorizontalAlign="Center" /><ItemTemplate><asp:Label ID="lblSerialNo" runat="server"></asp:Label></ItemTemplate></asp:TemplateField>

Grisview ASP

Step 3: add OnRowDataBound Properties in Gridview

<asp:GridView ID="GridView1" runat="server" CellPadding="4" Width="70%" AutoGenerateColumns="False" DataKeyNames="id" OnRowDataBound="GridView1_RowDataBound" AllowPaging="True" PageSize="15" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px">

Step 4: last and final step add below method in the C sharp code.


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

Step 5: Now your automatic Serial Number is ready whenever new contents are add in grid view it will automatically increase the serial number of your row.

🙂 if you Like Our Post Please Comment in Comment Section Thanks For Reading ……. 🙂

Read More Tutorials:

Related posts

One Thought to “How to Generate Serial Number in Gridview ASP

  1. […] How to Generate Serial Number in Gridview ASP […]

Leave a Comment