Gautam Buddha Temple Varanasi,Varanasi - Buddhist Pilgrimage

Monday, August 23, 2010

Create autogenerate serial number in Gridview

Auto Generate Serial Number In Gridview

The Given code auto generate serial number with page index changing.
Generate Dynamic Serial Number in GridView is coommon task for many programmer

Here i explain 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="gv" runat="server" DataKeyNames="Id">
<asp:TemplateField HeaderText="Sr. No">
<asp:Label ID="lblSrNo" runat="server"></asp:Label>
<ItemStyle HorizontalAlign="Center" Width="50px" />
<asp:TemplateField HeaderText="EmpName">
<asp:Label ID="lblEmpName" runat="server"></asp:Label>
<ItemStyle HorizontalAlign="Center" Width="50px" />
.cs code
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
Label lblSrNo = (Label)e.Row.FindControl("lblSrNo");
lblSrNo.Text = SrNo.ToString();
int pageNum = gv.PageIndex * gv.PageSize + Sr;
e.Row.Cells[1].Text = pageNum.ToString();

Thanks & Regards

1 comment:

  1. Hi
    This is really good article have a look of this also.