Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP
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 | :sigh:
 
help me
Posted 17-Feb-10 19:17pm
Edited 6-May-10 12:39pm
Moak11.4K
v2
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.Thumbs Up | :thumbsup:
  Permalink  
Comments
Monjurul Habib at 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 at 4-Dec-11 15:17pm
   
my 5!
andy(-RKO) at 20-Mar-13 6:53am
   
nice
Omkar Hendre at 15-Sep-14 5:46am
   
Thanks friend it works for me
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 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.. Thumbs Up | :thumbsup:
  Permalink  
v6
Comments
Monjurul Habib at 4-Dec-11 15:16pm
   
my 5!
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 at 27-Apr-14 15:05pm
   
You're 4 years late!
King_Fisher at 28-Apr-14 0:19am
   
:)

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 8,373
1 OriginalGriff 6,466
2 Peter Leow 3,567
3 Zoltán Zörgő 3,316
4 Richard MacCutchan 2,397


Advertise | Privacy | Mobile
Web03 | 2.8.150123.1 | Last Updated 27 Apr 2014
Copyright © CodeProject, 1999-2015
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