分享
 
 
 

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

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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

作者:孟宪会 出自:【孟宪会之精彩世界】 发布日期:2003年6月2日 3点39分15秒

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

查看例子

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

myaddress@mycompany.com

E章

孟子

0

youraddress@yourcompany.com

宪会

1

mm@mmm.mm

Lover

Net

0

xxx@xxxx.xx

NET开发者园地

0

hhh@hhh.hh

XML开发者园地

1

FormatDataGridVB.aspx

' runat="server" ID="Label1"

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 " Web 窗体设计器生成的代码 "

'该调用是 Web 窗体设计器所必需的。

Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init

'CODEGEN: 此方法调用是 Web 窗体设计器所必需的

'不要使用代码编辑器修改它。

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 = "格式化DataGrid的例子【将数据原中的0,1值转换成实际的文字】"

FormatDataGrid.Columns(0).HeaderText = "姓名"

FormatDataGrid.Columns(1).HeaderText = "电子邮件"

FormatDataGrid.Columns(2).HeaderText = "职位"

' 装载XML数据原,注意:这里与数据原类型没有关系,换成数据库也是适用的

_dsContacts = New DataSet()

_dsContacts.ReadXml(Server.MapPath("Contacts.xml"))

Dim dcPk As DataColumn() = {_dsContacts.Tables("Contact").Columns("Email")}

_dsContacts.Tables("Contact").PrimaryKey = dcPk

If Not Page.IsPostBack Then

' 只在页面首次请求时才进行数据绑定

BindContacts()

End If

End Sub

Private Sub BindContacts()

Dim dv As DataView = New DataView(_dsContacts.Tables("Contact"))

dv.Sort = "LastName, FirstName"

FormatDataGrid.DataSource = dv

FormatDataGrid.DataBind()

End Sub

Protected Function FormatFullName(ByVal FirstName As Object, ByVal LastName As Object) As String

' 格式划名称列

Return CType(LastName, String) & "." & 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, "Manager"), String)

If isManager = "1" Then

' 设定文字和背景颜色

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 = "普通员工"

End If

End If

End Sub

End Class

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 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 = "普通员工";

}

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有