Overblog Follow this blog
Edit post Administration Create my blog
Balavardhan Reddy Narani

Bind GridView With Data and Serial Number as First Column

March 7 2009 , Written by Balavardhan Published on #ASP.Net

Bind Serial Number as First Column to GridView

Add a Template Filed to Grid View

Ex:
<asp:TemplateField HeaderText="Ref.No">
<ItemTemplate>
<asp:Label ID="lblrefNo" runat="server" SkinID="PCRepeaterLabel" CssClass="mbody" Width="100">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>

Add OnRowDataBound Event to Grid View
Ex:
<asp:GridView ID="CollectedGridView" runat="server" OnRowDataBound ="CollectedGridView_RowDataBound">
</asp:GridView>


Add the Serial Numbering to Grid View Template Field in CollectedGridView_RowDataBound Event
Ex:
protected void CollectedGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
int _counter = 0;
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblref = (Label)(e.Row.FindControl("lblrefNo"));
lblref.Text = _counter.ToString() + 1;
lblref.DataBind();
}
}


If you have Paging for your Grid View

Declare a Global Variable
Ex:
int Index = 0

Set the Index value in Grid View Page Index Changing Event
Ex:
protected void CollectedGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
index = e.NewPageIndex * 10; //Grid View paging Value is 10
}

And add Index Value to Template Filed in RowDataBound
Ex:
protected void CollectedGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
int _counter = 0;
if (e.Row.RowType == DataControlRowType.DataRow)
{
_counter = index + 1;
Label lblref = (Label)(e.Row.FindControl("lblrefNo"));
lblref.Text = _counter.ToString();
lblref.DataBind();
index++;
}
}

Share this post

Repost 0

Comment on this post