格式化DataGrid的例子【將資料來源中的0,1值轉換成實際的文字】 (轉)

amyz發表於2007-08-15
格式化DataGrid的例子【將資料來源中的0,1值轉換成實際的文字】 (轉)[@more@]

格式化DataGrid的例子【將資料原中的0,1值轉換成實際的文字】

playground.com/">

下面的程式碼實現格式化DataGrid的列,也即是將資料原中的0,1值轉換成實際的文字的功能,主要是在資料繫結的幫定事件。

x">檢視例子

首先準備資料來源,資料來源採用、、陣列等都可以。下面以XML做例子。Contacts.xml如下:

<contacts> <contact> <e href="http://blog.itpub.net/10752019/viewspace-958424/tag-285-1.html">Mail&gt;myaddress@mycompany.comemail&gt; <firstname>E章</firstname> <lastname>孟子</lastname> <manager>0</manager> </e></contact> <contact> <email>youraddress@yourcompany.com</email> <firstname>憲會</firstname> <lastname>孟</lastname> <manager>1</manager> </contact> <contact> <email>mm@mmm.mm</email> <firstname>Lover</firstname> <lastname>Net</lastname> <manager>0</manager> </contact> <contact> <email>xxx@xxxx.xx</email> <firstname>NET開發者園地</firstname> <lastname> <manager>0</manager> </lastname></contact> <contact> <email>hhh@hhh.hh</email> <firstname>XML開發者園地</firstname> <lastname> <manager>1</manager> </lastname></contact> </contacts>

FormatDataGridVB.

.FormatDataGridVB&quot; %&gt; w3c//DTD HTML 4.0 Transitional//EN&quot; &gt; <meta name="GENERATOR" content="a href=" tag-126-1.html="">microsoft 7.0&quot;&gt; <meta name="CODE_LANGUAGE" content="a href=" tag-29-1.html="">&quot;&gt; <meta name="vs_defaultClientScript" content="a href=" tag-133-1.html="">script&quot;&gt; <meta name="vs_targetSchema" content=" href=" tag-340-1.html="">intellisense/&quot;&gt; <fo href="http://blog.itpub.net/10752019/viewspace-958424/tag-415-1.html">RM id=&quot;iSample&quot; method=&quot;post&quot; runat=&quot;server&quot; class=&quot;SubHeading&quot;&gt; 是 Web 窗體設計器所必需的。 <system href="http://blog.itpub.net/10752019/viewspace-958424/tag-268-1.html">buggerStepThrough()&gt; Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System., ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: 此方法呼叫是 Web 窗體設計器所必需的 '不要使用程式碼編輯器修改它。 InitializeComponent() End Sub #End Region Private _dntacts As DataSet Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load MyTitle.Text = &quot;格式化DataGrid的例子【將資料原中的0,1值轉換成實際的文字】&quot; FormatDataGrid.Columns(0).HeaderText = &quot;姓名&quot; FormatDataGrid.Columns(1).HeaderText = &quot;電子&quot; FormatDataGrid.Columns(2).HeaderText = &quot;職位&quot; ' 裝載XML資料原,注意:這裡與資料原型別沒有關係,換成資料庫也是適用的 _dsContacts = New DataSet() _dsContacts.ReadXml(Server.MapPath(&quot;Contacts.xml&quot;)) Dim dcPk As DataColumn() = {_dsContacts.Tables(&quot;Contact&quot;).Columns(&quot;Email&quot;)} _dsContacts.Tables(&quot;Contact&quot;).PrimaryKey = dcPk If Not Page.IsPostBack Then ' 只在頁面首次請求時才進行資料繫結 BindContacts() End If End Sub Private Sub BindContacts() Dim dv As DataView = New DataView(_dsContacts.Tables(&quot;Contact&quot;)) dv.Sort = &quot;LastName, FirstName&quot; FormatDataGrid.Data = dv FormatDataGrid.DataBind() End Sub Protected Function FormatFullName(ByVal FirstName As Object, ByVal LastName As Object) As String ' 格式劃名稱列 Return CType(LastName, String) &amp; &quot;.&quot; &amp; CType(FirstName, String) End Function Private Sub FormatDataGrid_ItemDataBound(ByVal sender As Object,_ ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles FormatDataGrid.ItemDataBound ' 確保處理的是資料行,而不是Header或者Footer If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then ' 得到Manager欄位的值 Dim isManager As String = CType(DataBinder.Eval(e.Item.DataItem, &quot;Manager&quot;), String) If isManager = &quot;1&quot; Then ' 設定文字和背景顏色 e.Item.Cells(2).Text = &quot;經理&quot; e.Item.Cells(2).Style.Add(&quot;font-weight&quot;, &quot;bold&quot;) e.Item.Cells(2).ForeColor = System.Drawing.Color.Red e.Item.BackColor = System.Drawing.Color.AliceBlue Else e.Item.Cells(2).Text = &quot;普通員工&quot; End If End If End Sub End Class </system></fo>

C#版本

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; /// <summary> /// Summary description for idbSample. /// </summary> public class idbSample : System.Web.UI.Page { #region Web Form Designer generated code overr protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.dgContacts.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgContacts_ItemDataBound); this.Load += new System.EventHandler(this.Page_Load); } #endregion protected System.Web.UI.WebControls.DataGrid FormatDataGrid; private DataSet _dsContacts; private void Page_Load(object sender, System.EventArgs e) { // 裝載XML資料原,注意:這裡與資料原型別沒有關係,換成資料庫也是適用的 _dsContacts = new DataSet(); _dsContacts.ReadXml(Server.MapPath(&quot;Contacts.xml&quot;)); DataColumn[] dcPk = {_dsContacts.Tables[&quot;Contact&quot;].Columns[&quot;Email&quot;]}; _dsContacts.Tables[&quot;Contact&quot;].PrimaryKey = dcPk; if (!Page.IsPostBack ) { BindContacts(); } } private void BindContacts() { DataView dv = new DataView(_dsContacts.Tables[&quot;Contact&quot;]); dv.Sort = &quot;LastName, FirstName&quot;; dgContacts.DataSource = dv; dgContacts.DataBind(); } protected string FormatFullName(object FirstName, object LastName) { // 格式劃名稱列 return (string)LastName + &quot;, &quot; + (string)FirstName; } protected void FormatDataGrid_ItemDataBound(object source, System.Web.UI.WebControls.DataGridItemEventArgs e) { // 確保處理的是資料行,而不是Header或者Footer if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { // 得到Manager欄位的值 string isManager = (string)DataBinder.Eval(e.Item.DataItem, &quot;Manager&quot;); if (isManager == &quot;1&quot;) { // ' 設定文字和背景顏色 e.Item.Cells[2].Text = &quot;經理&quot; e.Item.Cells[2].Style.Add(&quot;font-weight&quot;, &quot;bold&quot;) e.Item.Cells[2].ForeColor = System.Drawing.Color.Red e.Item.BackColor = System.Drawing.Color.AliceBlue } else { e.Item.Cells[2].Text = &quot;普通員工&quot;; } } } }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-958424/,如需轉載,請註明出處,否則將追究法律責任。

相關文章