Friday, December 18, 2009

Gridview Serial number

1. Add OnRowDataBound="GridView1_RowDataBound" inside your GridView declaration:

asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" ...

2. Add TemplateField inside your GridView:

asp:TemplateField HeaderText="Serial number"
ItemTemplate
asp:Label ID="lblSerial" runat="server">
/ItemTemplate
/asp:TemplateField

3. Add this in code-behind:

int i = 1;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblSerial = (Label)e.Row.FindControl("lblSerial");
lblSerial.Text = i.ToString();
i++;
}
}