在gridview中生成序列号 [英] Generate serial number in gridview

查看:88
本文介绍了在gridview中生成序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在gridview中生成我们自己的序列号?



例如我们在gridview中有10条记录,所以第一列应该代表序列号如1 2 3到10

:叹气:



帮助我

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

推荐答案

你可以通过使用数据库查询也像



ROW_NUMBER()OVER(ORDER BY ID)AS SNo
You can do this by using database query also like

ROW_NUMBER() OVER (ORDER BY ID) AS SNo


如果我的问题是正确的,你就是这样做的:

1.在GridView声明中添加OnRowDataBound =GridView1_RowDataBound:

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.在你的内部添加TemplateField GridView:


2. Add TemplateField inside your GridView:

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



3.在代码隐藏中添加此项:


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();
        }
    }





希望这会有所帮助。:upumbsup:



Hope this helps.:thumbsup:


这就是你要找的。简单,轻松,轻便。只需使用这个代码,你就可以完成这样的工作........它也可以在分页中工作......



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


这篇关于在gridview中生成序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆