日期:2010-05-19 浏览次数:20519 次
下面的代码实现格式化DataGrid的列,也即是将数据原中的0,1值转换成实际的文字的功能,主要是在数据绑定的帮定事件。
查看例子
首先准备数据源,数据源采用数据库、XML、数组等都可以。下面以XML做例子。Contacts.xml文件如下:
<?xml version="1.0" encoding="gb2312" standalone="yes"?>
<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>
</Contact>
<Contact>
<Email>hhh@hhh.hh</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">