45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:格式化DataGrid的例子的介绍

格式化DataGrid的例子的介绍

2016-08-31 03:25:00 来源:www.45fan.com 【

格式化DataGrid的例子的介绍

格式化DataGrid的例子【将数据原中的0,1值转换成实际的文字】

http://lucky_elove.www1.dotnetplayground.com/

下面的代码实现格式化DataGrid的列,也即是将数据原中的0,1值转换成实际的文字的功能,主要是在数据绑定的帮定事件。

查看例子

首先准备数据源,数据源采用数据库、XML、数组等都可以。下面以XML做例子。Contacts.xml文件如下:

<contacts> <contact> <email>myaddress@mycompany.com</email> <firstname>E章</firstname> <lastname>孟子</lastname> <manager>0</manager> </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><lastname/> <contact> <email>hhh@hhh.hh</email> <firstname>XML开发者园地</firstname> <lastname/> <manager>1</manager> </lastname/></contact><lastname/> </lastname/></lastname/></contacts><lastname/><lastname/> </lastname/></lastname/>

FormatDataGridVB.aspx

<%@ autoeventwireup="false" codebehind="FormatDataGridVB.aspx.vb" inherits="aspxWeb.FormatDataGridVB" language="vb" page=""> <!doctype 4.0="" dtd="" html="" public=""> <html> <head> <meta content="Microsoft Visual Studio 7.0" 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 class="SubHeading" id="idbSample" method="post" runat="server"> <asp:label font-bold="True" id="MyTitle" runat="server"></asp:label><br /> <br /> <asp:datagrid autogeneratecolumns="False" id="FormatDataGrid" runat="server" width="100%"> <headerstyle font-bold="True"></headerstyle> <columns> <asp:templatecolumn> <itemtemplate> <asp:label id="Label1" runat="server" text="&lt;%# FormatFullName(DataBinder.Eval(Container, &quot;DataItem.FirstName&quot;),_ DataBinder.Eval(Container, &quot;DataItem.LastName&quot;)) %&gt;"> </asp:label> </itemtemplate> </asp:templatecolumn> <asp:boundcolumn datafield="Email" readonly="True"></asp:boundcolumn> <asp:boundcolumn datafield="Manager" readonly="True"> <headerstyle horizontalalign="Center"></headerstyle> <itemstyle horizontalalign="Center"></itemstyle> </asp:boundcolumn> </columns> </asp:datagrid></form> </body> </html> </!doctype></%@><%@ page="" language="vb" autoeventwireup="false" codebehind="FormatDataGridVB.aspx.vb" inherits="aspxWeb.FormatDataGridVB">

FormatDataGridVB.aspx.vb

Imports System Imports System.Data Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Xml Public Class FormatDataGridVB Inherits System.Web.UI.Page Protected WithEvents FormatDataGrid As System.Web.UI.WebControls.DataGrid Protected WithEvents MyTitle As System.Web.UI.WebControls.Label #Region &quot; Web 窗体设计器生成的代码 &quot; &#39;该调用是 Web 窗体设计器所必需的。 <system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init &#39;CODEGEN: 此方法调用是 Web 窗体设计器所必需的 &#39;不要使用代码编辑器修改它。 InitializeComponent() End Sub #End Region Private _dsContacts 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; &#39; 装载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 &#39; 只在页面首次请求时才进行数据绑定 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.DataSource = dv FormatDataGrid.DataBind() End Sub Protected Function FormatFullName(ByVal FirstName As Object, ByVal LastName As Object) As String &#39; 格式划名称列 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 &#39; 确保处理的是数据行,而不是Header或者Footer If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then &#39; 得到Manager字段的值 Dim isManager As String = CType(DataBinder.Eval(e.Item.DataItem, &quot;Manager&quot;), String) If isManager = &quot;1&quot; Then &#39; 设定文字和背景颜色 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.diagnostics.debuggerstepthrough()>

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 override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET 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;) { // &#39; 设定文字和背景颜色 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;; } } } }
 

本文地址:http://www.45fan.com/a/question/70032.html
Tags: 格式化 DataGrid 数据源
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部