Auto Generated Serial Number In Gridview Control

Auto Generated Serial Number In Gridview Control

Generate Dynamic Serial Number in GridView

Generate Dynamic Serial Number in GridView is coommon task for many programmer,so here i am explaining it in very simple way.
1: Drag GridView in you web page.
2: Go to Gridview properties and Click on Columns than add Template Column.
3:Code is given below

<asp:GridView ID="gvIns" runat="server" DataKeyNames="InstituteId" AllowPaging="True" PageSize="12"
AutoGenerateColumns="False" OnPageIndexChanging="gvInstitute_PageIndexChanging">
<asp:TemplateField HeaderText="Sr. No">
<asp:Label ID="lblSrNo" runat="server"> </asp:Label>
<ItemStyle HorizontalAlign="Center" Width="50px" />
<asp:TemplateField HeaderText="Inst">
<asp:Label ID="lblIns" runat="server" Text='<%# Bind("Inst") %> '> </asp:Label>
<asp:TextBox ID="txtInst" runat="server" Text='> %# Bind("Inst") %> '> </asp:TextBox>
<asp:TextBox ID="txtNewInst" runat="server" Visible="false"> </asp:TextBox>
<FooterStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />

protected void gvInst_RowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
Label lblSrNo = (Label)e.Row.FindControl("lblSrNo");
lblSrNo.Text = SrNoQua.ToString();

int pageNum = gvInst.PageIndex * gvInst.PageSize + SrNoInst;
e.Row.Cells[1].Text = pageNum.ToString();

Related Articles

More articles: Gridview autogenerated number Gridview databind Gridview add rows


Guest Author: venkat28 Feb 2012

this type of codes is very useful to fresher like as me

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: