日期:2012-05-07  浏览次数:20472 次

下面的代码实现格式化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="aspxWeb.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="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<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 WithEvent