格式化 DataGrid 输出

** 格式化 DataGrid 输出 **

翻译: [email protected]

在这个论坛中,我看到了大量的关于如何在DataGrid中格式化显示数据的问题。人们想知道在特殊情况下,怎样改变某行的颜色,文字或基本的显示值 。 在这篇文章中,我将使用两种不同的技术向您演示如何修改您的数据的输出格式。我将向你展示怎样使用helper函数来改变数据数据显示格式。在这之后,我将解释怎样使用DataGrid的 OnItemDataBound事件来修改DataGrid的数据并格式化显示它。

计划

首先,我设计了这个示例中的数据文件,它是一个简单的关于联系人的XML文件。我的DataGird中的显示需求包括:

  • 合并首尾姓名。
  • 如果Manager的值是1或Employee的值是0时改变Manager的显示方式。
  • 如果Manager字段中有任一一行的值是1,则改变该行的背景色。
 1<contacts>
 2<contact>
 3<email>[email protected]</email>
 4<firstname>John</firstname>
 5<lastname>Doe</lastname>
 6<manager>0</manager>
 7</contact>
 8<contact>
 9<email>[email protected]</email>
10<firstname>Jane</firstname>
11<lastname>Doe</lastname>
12<manager>1</manager>
13</contact>
14<contact>
15<email>[email protected]</email>
16<firstname>Fred</firstname>
17<lastname>Flintstone</lastname>
18<manager>0</manager>
19</contact>
20<contact>
21<email>[email protected]</email>
22<firstname>Barney</firstname>
23<lastname>Rubble</lastname>
24<manager>0</manager>
25</contact>
26<contact>
27<email>[email protected]</email>
28<firstname>Mr</firstname>
29<lastname>Slate</lastname>
30<manager>1</manager>
31</contact>
32</contacts>

接下来设置数据网格。我想用 First Name, Last Name 格式将名字组合到一列中,因此我使用 TemplateColumn 。其它两列,每列简单的绑定在一个字段 ,因此用 BoundColumn 就足够了。

 1<asp:datagrid autogeneratecolumns="False" id="dgContacts" runat="server" width="370px">
 2<itemstyle cssclass="item"></itemstyle>
 3<headerstyle cssclass="heading"></headerstyle>
 4<columns>
 5<asp:templatecolumn headertext="Name">
 6<itemtemplate>
 7<asp:label runat="server" text='```
 8# DataBinder.Eval(Container, "DataItem.FirstName") 
 9```'>
10</asp:label>
11</itemtemplate>
12</asp:templatecolumn>
13<asp:boundcolumn datafield="Email" headertext="Email" readonly="True"></asp:boundcolumn>
14<asp:boundcolumn datafield="Manager" headertext="Status" readonly="True">
15<headerstyle horizontalalign="Center"></headerstyle>
16<itemstyle horizontalalign="Center"></itemstyle>
17</asp:boundcolumn>
18</columns>
19</asp:datagrid>

哪个方法?

现在,需要显示网格数据,我需要决定使用哪个方法来显示这些数据。 使用 helper 函数是一种非常容易的改变数据输出的方法。 为了改变网格的格式,应使用 ItemDataBound 事件。 大脑中应记住这些规则,下面是这决定要做的事情:

方法说明
NameHelper输出需要简单的组合 FirstName 和 LastName 字段.
EmailNone不做任何改变.
StatusItemDataBound需要改变基于 Manager 字段值的行的背景色。另外,需要改变显示基于 Manager 字段值的 ManagerEmployee 。第二部分能够使用 helper 函数很容易的实现,但是因为我已经使用了 ItemDataBound 事件,因此我决定在同一个位置做两次操作。

Helper 函数

设置 helper 函数非常简单。 首先,我在代码区创建了这个函数。 注意:函数作用域被声明为 protected. 这是必需的,因为函数将会在代码后的ASPX页中被调用。 去掉函数的私有属性将会在分析页面时出现错误。

[C#]
protectedstring FormatFullName( object FirstName, object LastName)
{
// Combine the names to get the Last, First format.
return ( string )LastName + ", " + ( string )FirstName;
}

[VB]
ProtectedFunction FormatFullName( ByVal FirstName AsObject , ByVal LastName AsObject ) AsString
' Combine the names to get the Last, First format.
ReturnCType (LastName, String ) & ", " & CType (FirstName, String )
EndFunction

为了从网格中调用函数,我改变了 TemplateColumn 中标签的文本值。

 1<asp:label runat="server" text='```
 2# FormatFullName(DataBinder.Eval(Container, "DataItem.FirstName"), DataBinder.Eval(Container, "DataItem.LastName")) 
 3```'>
 4
 5ItemDataBound 事件 
 6
 7然后,我设置了网格的  ItemDataBound  事件。有一件事你必须记住:当使用这个事件时,它会触发DataGrid中已建立的每一行。 这包含了页首及页尾行! 结果是,你必须确信你正确的设置了数据行的类型。 这由检查Item对象的  ItemType 属性来完成。  我无法告诉你当我试着存取数据行中已存在的控件时,曾经遇到过多少次运行时错误,但页首中就不存在这样的情况。 
 8
 9[C#]    
10protectedvoid  dgContacts_ItemDataBound(  object  source, System.Web.UI.WebControls.DataGridItemEventArgs e)   
11{   
12// 确定是数据行而非页首或页尾   
13if  (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)   
14{   
15// 取得 manager 字段的值   
16string  isManager = (  string  )DataBinder.Eval(e.Item.DataItem,  "Manager"  );   
17  
18if  (isManager ==  "1"  )   
19{   
20// 设置文本及背景颜色.   
21e.Item.Cells[2].Text =  "Manager"  ;   
22e.Item.BackColor = System.Drawing.Color.AliceBlue;   
23}   
24else    
25{   
26// 仅设置文本.   
27e.Item.Cells[2].Text =  "Employee"  ;   
28}   
29}   
30}   
31  
32[VB]   
33PrivateSub  dgContacts_ItemDataBound(  ByVal  sender  AsObject  ,  ByVal  e  As  System.Web.UI.WebControls.DataGridItemEventArgs)  Handles  dgContacts.ItemDataBound   
34' 确定是数据行而非页首或页尾   
35If  e.  Item  .ItemType = ListItemType.  Item  OrElse  e.  Item  .ItemType = ListItemType.AlternatingItem  Then    
36' 取得 manager 字段的值   
37Dim  isManager  AsString  =  CType  (DataBinder.Eval(e.  Item  .DataItem,  "Manager"  ),  String  )   
38  
39If  isManager =  "1"  Then    
40' 设置文本及背景颜色.   
41e.  Item  .Cells(2).Text =  "Manager"    
42e.  Item  .BackColor = System.Drawing.Color.AliceBlue   
43Else    
44' 仅设置文本.   
45e.  Item  .Cells(2).Text =  "Employee"    
46EndIf    
47EndIf    
48EndSub    
49
50
51**结果**
52
53最后,我们总结一下,看看已经创建了什么! 如果我已经正确的完成了每件工作,我将可以获得一个包含了3列的简单网格。名字由 last name 和 first name 组合而成。接着是电子邮件地址,然后是状态列指示此人是否是管理员或雇员。 最后,任何显示为管理人员的行都有背景色,让我们看看最终的运行结果! 
54
55![](http://www.csdn.net/editor/1.gif)
56
57**摘要**
58
59正如你所看到的,有多种不同的方法可以改变你的DataGrid中的数据输出格式。 你所选择的方法依赖于你需要执行的操作或你考虑的方式。 以上方法都非常简单,当你使用它们的时候,就会增加DataGrid控件的可有性。</asp:label>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus