格式化DataGrid的例子【将数据源中的0,1值或者逻辑值转换成实际的文字】
[入库:2005年8月18日] [更新:2007年3月24日]
| 格式化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 <%@ 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 " 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 = "普通员工";
}
}
}
}
|
本文关键:格式化DataGrid的例子【将数据源中的0,1值或者逻辑值转换成实际的文字】
本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)