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 18:17pm
Edited 6-May-10 11:39am
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
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 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 6

Thanks for ..Indrajeet Sutar7.1K
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 457
1 OriginalGriff 430
2 Shai Vashdi 240
3 Emre Ataseven 165
4 Peter Leow 146
0 Sergey Alexandrovich Kryukov 8,944
1 OriginalGriff 5,280
2 Peter Leow 3,985
3 Maciej Los 3,535
4 Abhinav S 3,218


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 29 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid