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 asp.net 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">
<Columns>
<asp:TemplateField HeaderText="Sr. No">
<ItemTemplate>
<asp:Label ID="lblSrNo" runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="50px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="EmpName">
<ItemTemplate>
<asp:Label ID="lblEmpName" runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="50px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
.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();
SrNo++;
int pageNum = gv.PageIndex * gv.PageSize + Sr;
e.Row.Cells[1].Text = pageNum.ToString();
}
}
Thanks & Regards
Santosh
Hi
ReplyDeleteThis is really good article have a look of this also.
http://www.dotnetpools.com/2013/10/auto-generate-row-number-in-gridview-in.html