惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News | PayPal Newsroom
云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Register - Security
The Register - Security
L
LangChain Blog
A
About on SuperTechFans
S
Schneier on Security
博客园 - 三生石上(FineUI控件)
Stack Overflow Blog
Stack Overflow Blog
The Hacker News
The Hacker News
AWS News Blog
AWS News Blog
博客园 - 司徒正美
Scott Helme
Scott Helme
K
Kaspersky official blog
Cyberwarzone
Cyberwarzone
T
Tenable Blog
腾讯CDC
Recorded Future
Recorded Future
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
G
GRAHAM CLULEY
Security Latest
Security Latest
S
Securelist
D
Darknet – Hacking Tools, Hacker News & Cyber Security
aimingoo的专栏
aimingoo的专栏
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
雷峰网
雷峰网
T
The Exploit Database - CXSecurity.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX
T
The Blog of Author Tim Ferriss
D
Docker
S
Security Affairs
F
Full Disclosure
Know Your Adversary
Know Your Adversary
N
News and Events Feed by Topic
N
News and Events Feed by Topic
T
Tor Project blog
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Microsoft Security Blog
Microsoft Security Blog
Simon Willison's Weblog
Simon Willison's Weblog
Recent Announcements
Recent Announcements
博客园_首页
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Security @ Cisco Blogs

博客园 - 秋枫

使用BizTalk Server常见问题处理 BizTalk Server中简单xml架构使用举例 CodeProject.com上微软BizTalk平台技术文章集锦 BizTalk Server 2006中使用MQSeries适配器问题处理 基于.Net平台应用程序唯一运行实例实现 使用BizTalk Server的Sql适配器出现“新事务不能登记到指定的事务处理器中”异常的处理 谈基于.net平台windows开发中的模式窗体 Visual studio 2005下xml的xsl转换调试 小提国内一些提供RSS服务的网站生成Rss文件问题 Visual studio .net 的“隐蔽性”和容易“忽略”的功能 使用sqlcmd(osql) 实用工具列出本地网内的Sql Server 服务器 《敏捷注册表》1.0使用说明书 《敏捷注册表》1.0概述 使用从BindingManagerBase.PositionChanged 事件的注意点 使用Microsoft SQL Server 2000的XML查询 使用.net framewrok 1.1版System.Windows.Forms.TreeView的一个问题 使用.net framewrok 1.1版注册表API的局限和解决 使用SaveFileDialog需要注意的情况 使用.net framework中常用类在2.0版中的新功能
asp.net中显示DataGrid控件列序号的几种方法
秋枫 · 2005-10-08 · via 博客园 - 秋枫

aps.net中多数据绑定的控件很多,论功能来说,应该属DataGrid最为齐全,但它没有提供现成的显示记录序号的功能,不过我们可以通过它所带的一些参数来间接得到序号,下面来看看怎样得到和显示序号值计算方式如下:

(1)在后台

DataGrid.CurrentPageIndex * DataGrid.PageSize + e.Item.ItemIndex + 1

(2)在前台

DataGrid1.CurrentPageIndex * DataGrid1.PageSize + Container.ItemIndex + 1

说明:

e表示System.Web.UI.WebControls.DataGridItemEventArgs参数类的实例;

DataGrid1这里表示前台的一个实例;

DataGrid.CurrentPageIndex:获取或设置当前显示页的索引;

DataGrid.PageSize :获取或设置要在 DataGrid 控件的单页上显示的项数。

下面我使用了4种方法来在前台显示序号,不过都是围绕上面的计算式展开。

(1)         使用DataGridItemCreated设置值,而前台的单元格可以是绑定列或者模板列(包括空模板)

(2)         使用DataGridItemDataBound设置值,而前台的单元格可以是绑定列或者模板列(包括空模板)

(3)         在前台直接绑定计算表达式;

(4)         在后台类中编写方法计算表达式由前台页面类继承调用。

备注:在数据库中获取数据时设置额外的序号列这里不做讨论,我认为这是最糟糕的实现方法。

下面以获取Northwind数据库的Customers表的数据为列,显示如下:

序号1

序号2

序号3

序号4

序号5

CustomerID

51

51

51

51

51

LONEP

52

52

52

52

52

MAGAA

53

53

53

53

53

MAISD

54

54

54

54

54

MEREP

55

55

55

55

55

MORGK

56

56

56

56

56

NORTS

57

57

57

57

57

OCEAN

58

58

58

58

58

OLDWO

59

59

59

59

59

OTTIK

60

60

60

60

60

PARIS

1 2 3 4 5 6 7 8 9 10

下面是WebFormPaging.aspx文件代码,

<%@ Page language="c#" Codebehind="WebFormPaging.aspx.cs" AutoEventWireup="false" Inherits="AspnetPaging.WebForm1" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

         <HEAD>

                   <title>WebForm1</title>

                   <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">

                   <meta content="C#" name="CODE_LANGUAGE">

                   <meta content="JavaScript" name="vs_defaultClientScript">

                   <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">

         </HEAD>

         <body>

                   <form id="Form1" method="post" runat="server">

                            <TABLE id="Table1" cellSpacing="1" cellPadding="1" width="400" align="center" border="1">

                                     <TR>

                                               <TD><asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" Width="100%" AllowPaging="True">

                                                                 <Columns>

                                                                           <asp:BoundColumn HeaderText="序号1"></asp:BoundColumn>

                                                                           <asp:TemplateColumn HeaderText="序号2"></asp:TemplateColumn>

                                                                           <asp:TemplateColumn HeaderText="序号3">

                                                                                    <ItemTemplate>

                                                                                             <asp:Label ID="itemIndex" runat="server"></asp:Label>

                                                                                    </ItemTemplate>

                                                                           </asp:TemplateColumn>

                                                                           <asp:TemplateColumn HeaderText="序号4">

                                                                                    <ItemTemplate>

                                                                                             <%# (DataGrid1.CurrentPageIndex * DataGrid1.PageSize + Container.ItemIndex + 1) %>

                                                                                    </ItemTemplate>

                                                                           </asp:TemplateColumn>

                                                                           <asp:TemplateColumn HeaderText="序号5">

                                                                                    <ItemTemplate>

                                                                                             <%# GetRecordIndex( Container.ItemIndex ) %>

                                                                                    </ItemTemplate>

                                                                           </asp:TemplateColumn>

                                                                           <asp:BoundColumn DataField="CustomerID" HeaderText="CustomerID"></asp:BoundColumn>

                                                                 </Columns>

                                                                 <PagerStyle Mode="NumericPages"></PagerStyle>

                                                        </asp:datagrid></TD>

                                     </TR>

                                     <TR>

                                               <TD></TD>

                                     </TR>

                                     <TR>

                                               <TD></TD>

                                     </TR>

                            </TABLE>

                   </form>

         </body>

</HTML>

后台WebFormPaging.aspx.cs代码如下:

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

 //zhzuo 2004-9-10

namespace AspnetPaging

{

     public class WebForm1 : System.Web.UI.Page

     {

         private int recordCount = 0;

         protected System.Web.UI.WebControls.DataGrid DataGrid1;

         private void Page_Load(object sender, System.EventArgs e)

         {

              if(!Page.IsPostBack)

              {

                   DataGridDataBind();

              }

         }

         //绑定数据

         private void DataGridDataBind()

         {

              DataSet ds = DataAccess.GetCustomersData();

              this.DataGrid1.DataSource = ds;

              this.DataGrid1.DataBind();

         }

         #region Web 窗体设计器生成的代码

         override protected void OnInit(EventArgs e)

         {

              InitializeComponent();

              base.OnInit(e);

         }

         /// <summary>

         /// 设计器支持所需的方法 - 不要使用代码编辑器修改

         /// 此方法的内容。

         /// </summary>

         private void InitializeComponent()

         {   

              this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreated);

              this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);

              this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);

              this.Load += new System.EventHandler(this.Page_Load);

         }

         #endregion

         //翻页

         private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)

         {

              DataGrid1.CurrentPageIndex = e.NewPageIndex;

              DataGridDataBind();

         }

         //获取当前项

         protected int GetRecordIndex(int itemIndex)

         {

              return (DataGrid1.CurrentPageIndex * DataGrid1.PageSize + itemIndex + 1);

         }

                   private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

         {

              DataGrid dg = (DataGrid)sender;

              if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

              {

                   e.Item.Cells[0].Text = (dg.CurrentPageIndex * dg.PageSize + e.Item.ItemIndex + 1).ToString();

                   e.Item.Cells[1].Text = (dg.CurrentPageIndex * dg.PageSize + e.Item.ItemIndex + 1).ToString();

              }

         }

         private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

         {

              DataGrid dg = (DataGrid)sender;

              if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

              {

                   ((Label)e.Item.FindControl("itemIndex")).Text = (dg.CurrentPageIndex * dg.PageSize + e.Item.ItemIndex + 1).ToString();

              }

         }

     }

}

数据层代码如下:

using System;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

namespace AspnetPaging

{

     public class DataAccess

     {

         private static string connString = ConfigurationSettings.AppSettings["ConnString"];

         private DataAccess()

         {

          }

         public static DataSet GetCustomersData()

         {

              SqlConnection conn = new SqlConnection(connString);

              SqlCommand comm = new SqlCommand("GetCustomers",conn);

              comm.CommandType = CommandType.StoredProcedure;

              SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);

              DataSet ds = new DataSet();

              dataAdapter.Fill(ds);

              return ds;

         }

     }

}

总结,上面的四种方法前两种其实处理起来是一样的,只是处理的时间不同而已;对于第三种我认为最简单,直接在前台页面绑定,不需要额外的辅助;对于第四种的方法绑定到前台我认为最为灵活,需要注意的是GetRecordIndex方法需要protectedpublic,使它的继承类能访问的到。