Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
How to generate our own serial number in gridview?

eg we have ten records in gridview so first col should represents serial no Like 1 2 3 to 10
:sigh:

help me
Posted
Updated 5-May-18 22:00pm
v2

You can do this by using database query also like

ROW_NUMBER() OVER (ORDER BY ID) AS SNo
 
Share this answer
 
If I am getting your question right, this is how you do it:
1. Add OnRowDataBound="GridView1_RowDataBound" inside your GridView declaration:
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" ...

2. Add TemplateField inside your GridView:
XML
<asp:TemplateField HeaderText="Serial number">
    <ItemTemplate>
        <asp:Label ID="lblSerial" runat="server"></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

3. Add this in code-behind:
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblSerial = (Label)e.Row.FindControl("lblSerial");
            lblSerial.Text = ((GridView1.PageIndex * GridView1.PageSize) + e.Row.RowIndex + 1).ToString();
        }
    }


Hope this helps.:thumbsup:
 
Share this answer
 
Comments
Monjurul Habib 4-Dec-11 13:42pm    
my 5!
This is what you are looking for. Simple, easy and lightweight . Just use this code and you will get it done just like that........ and it will work in paging also...

C#
<asp:TemplateField>
 <HeaderTemplate>
 Serial No.</HeaderTemplate>
 <itemtemplate>
 <asp:Label ID="lblSRNO" runat="server" Text='<%#Container.DataItemIndex+1 %>'>
 </itemtemplate>


you can ask me any questions i will try my hand on that....

tell me after you done....

happy coding
 
Share this answer
 
v3
Comments
Monjurul Habib 4-Dec-11 15:17pm    
my 5!
jazygift 23-Aug-18 6:24am    
what are you trying to say njobu ?
andy(-RKO) 20-Mar-13 6:53am    
nice
Omkar Hendre 15-Sep-14 5:46am    
Thanks friend it works for me
html code inside gridview


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




and at page load..


where you are binding the data to the gridview..

use a foreach loop...



like

int i=1;
foreach(gridviewrow row in id of gridview.rows)
{
   label lbl = (label)row.findcontrol("lblserial");
   lbl.text= i;
   i++;
}



hope this will help.. :thumbsup:
 
Share this answer
 
v6
Comments
Monjurul Habib 4-Dec-11 15:16pm    
my 5!
jazygift 23-Aug-18 6:15am    
what ?
XML
<asp:TemplateField HeaderText="sl.no" >
        <ItemTemplate >
             <%#Container.DataItemIndex+1 %>
        </ItemTemplate>
     </asp:TemplateField>
 
Share this answer
 
Comments
CHill60 27-Apr-14 15:05pm    
You're 4 years late!
King Fisher 28-Apr-14 0:19am    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900