asp.net中顯示DataGrid控制項列序號的幾種方法

來源:互聯網
上載者:User
asp.net|datagrid|datagrid控制項|顯示 asp.net中顯示DataGrid控制項列序號的幾種方法

作者:鄭佐 2004-9-10

在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) 使用DataGrid的ItemCreated設定值,而前台的儲存格可以是繫結資料行或者模板列(包括空模板);

(2) 使用DataGrid的ItemDataBound設定值,而前台的儲存格可以是繫結資料行或者模板列(包括空模板);

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



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 Form設計器產生的程式碼

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方法需要protected或public,使它的繼承類能訪問的到。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.