下面的代码实现格式化datagrid的列,也即是将数据原中的0,1值转换成实际的文字的功能,主要是在数据绑定的帮定事件。
查看例子
首先准备数据源,数据源采用数据库、xml、数组等都可以。下面以xml做例子。contacts.xml文件如下:
<?xml version="1.0" encoding="gb2312" standalone="yes"?>
<contacts>
<contact>
<email>[email protected]</email>
<firstname>e章</firstname>
<lastname>孟子</lastname>
<manager>0</manager>
</contact>
<contact>
<email>[email protected]</email>
<firstname>宪会</firstname>
<lastname>孟</lastname>
<manager>1</manager>
</contact>
<contact>
<email>[email protected]</email>
<firstname>lover</firstname>
<lastname>net</lastname>
<manager>0</manager>
</contact>
<contact>
<email>[email protected]</email>
<firstname>net开发者园地</firstname>
<lastname/>
<manager>0</manager>
</contact>
<contact>
<email>[email protected]</email>
<firstname>xml开发者园地</firstname>
<lastname/>
<manager>1</manager>
</contact>
</contacts>
formatdatagridvb.aspx
<%@ page language="vb" autoeventwireup="false" codebehind="formatdatagridvb.aspx.vb"
inherits="aspx<a href="http://dev.21tx.com/web/" target="_blank">web</a>.formatdatagridvb" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<meta name="generator" content="microsoft visual studio 7.0">
<meta name="code_language" content="<a href="http://dev.21tx.com/dotnet/csharp/" target="_blank">c#</a>">
<meta name="vs_defaultclientscript" content="<a href="http://dev.21tx.com/web/javascript/" target="_blank">javascript</a>">
<meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body>
<form id="idbsample" method="post" runat="server" class="subheading">
<asp:label id="mytitle" runat="server" font-bold="true"></asp:label>
<br>
<br>
<asp:datagrid id="formatdatagrid" runat="server" autogeneratecolumns="false" width="100%">
<headerstyle font-bold="true"></headerstyle>
<columns>
<asp:templatecolumn>
<itemtemplate>
<asp:label
text='<%# formatfullname(databinder.eval(container, "dataitem.firstname"),_
databinder.eval(container, "dataitem.lastname")) %>'
runat="server" id="label1">
</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>
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 窗体设计器所必需的。
<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
'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>
/// 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("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 = "普通员工";
}
}
}
}
新闻热点
疑难解答
图片精选