格式化DataGrid的例子

时间:2010-03-09 11:59:24  来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:可以。下面以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> </Cont...
  正文:下面的代码实现格式化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.ASP/ target=_blank class=infotextkey>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 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

格式化DataGrid的例子》由第二电脑网原创提供,转载请注明:http://www.002pc.com/master/College/Programming/aspnet/13143.html


关键字:

关于《格式化DataGrid的例子》文章的评论

站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td