Click here to Skip to main content
13,247,674 members (63,801 online)
Rate this:
 
Please Sign up or sign in to vote.
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 17-Feb-10 19:17pm
Updated 26-Mar-17 2:09am
Moak11.6K
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

You can do this by using database query also like

ROW_NUMBER() OVER (ORDER BY ID) AS SNo
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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:
<asp:TemplateField HeaderText="Serial number">
    <ItemTemplate>
        <asp:Label ID="lblSerial" runat="server"></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

3. Add this in code-behind:
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:
  Permalink  
Comments
Monjurul Habib 4-Dec-11 13:42pm
   
my 5!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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...

<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
  Permalink  
v3
Comments
Monjurul Habib 4-Dec-11 15:17pm
   
my 5!
andy(-RKO) 20-Mar-13 6:53am
   
nice
Omkar Hendre 15-Sep-14 5:46am
   
Thanks friend it works for me
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 8

<asp:TemplateField HeaderText="sl.no" >
        <ItemTemplate >
             <%#Container.DataItemIndex+1 %>
        </ItemTemplate>
     </asp:TemplateField>
  Permalink  
Comments
CHill60 27-Apr-14 15:05pm
   
You're 4 years late!
King_Fisher 28-Apr-14 0:19am
   
:)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

html code inside gridview


<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:
  Permalink  
v6
Comments
Monjurul Habib 4-Dec-11 15:16pm
   
my 5!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.171114.1 | Last Updated 26 Mar 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100