| 
 | 
 
       按实际要求格式化显示DataGrid中字段值的方法_[Asp.Net教程]                                           源程序:   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 description for idbSample.      ///       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);      }           ///       /// Required method for Designer support - do not modify      /// the contents of this method with the code editor.      ///       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("Contacts.XML"));      DataColumn[] dcPk = {_dsContacts.Tables["Contact"].Columns["Email"]};      _dsContacts.Tables["Contact"].PrimaryKey = dcPk;           if (!Page.IsPostBack )      {      BindContacts();      }      }           private void BindContacts()      {      DataView dv = new DataView(_dsContacts.Tables["Contact"]);      dv.Sort = "LastName, FirstName";      dgContacts.DataSource = dv;      dgContacts.DataBind();      }           protected string FormatFullName(object FirstName, object LastName)      {      // 格式划名称列      return (string)LastName + ", " + (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, "Manager");           if (isManager == "1")      {      // ’ 设定文字和背景颜色      e.Item.Cells[2].Text = "经理"      e.Item.Cells[2].Style.Add("font-weight", "bold")      e.Item.Cells[2].ForeColor = System.Drawing.Color.Red      e.Item.BackColor = System.Drawing.Color.AliceBlue      }      else      {      e.Item.Cells[2].Text = "普通员工";      }      }      }      }                                               
 
 
 
 |