在本文的第一部分我们研究了如何设定DataGrid Web控件的显示属性以及如何通过样式设定DataGrid的页眉页脚行和交替行的显示所有这些技术或是用于设定整个DataGrid的显示或是用于设定DataGrid中行的显示但是如何设定DataGrid中列的显示属性?其实并不难接着读你就知道了
设定哪些列应该显示
缺省情况下DataGrid在生成的HTML表格中为SQL查询返回的每一列生成一个对应的列但是在一些情况下仅希望在DataGrid中显示这些列中的一部分列例如在我正在进行的示例中通过调用sp_Popularity存储过程显示了最受欢迎的个问题它包含FAQID列或许我并不希望显示该列
如果不想在DataGrid中显示数据库查询返回的所有列必须显式地声明所有希望显示的列第一步是将DataGrid的AutoGenerateColumns属性设为False一旦执行完这个操作就需要通过BoundColumn Web控件设定需显示的列如下所示
<asp:DataGrid runat=server AutoGenerateColumns=False>
<Columns>
<asp:BoundColumn DataField=DatabaseColumnName />
<asp:BoundColumn DataField=DatabaseColumnName />
<asp:BoundColumn DataField=DatabaseColumnNameN />
</Columns>
</asp:datagrid>
对于每一个希望显示的列需要通过一个包含DataField属性的<asp:BoundColumn />标记来指定数据库中需要显示的列所有这些BoundColumn标记必须包含在Column标记内(也可通过编程的方式指定这些绑定列但是它的可读性差并且需要很多代码!)请注意只有通过BoundColumn标记指定的列才会在DataGrid中显示你必须指定需要显示的列!
BoundColumn控件的优点在于它包含一些设定格式的属性包括
lHeaderText — 设定列标题的文字
lFooterText — 设定列尾的文字(记住若要在DataGrid中显示页脚应将ShowFooter设为True)
lHeaderStyle/FooterStyle/ItemStyle — 包含与DataGrid样式相同的属性对设定列居中前景色背景色等很有用
lDataFormatString — 设置格式命令(参考下面的示例参考文档以获得全部的格式化规范)
让我们看一下如何通过使用BoundColumn标记来进一步增强前面的示例正如前面所提到的我们不想显示FAQID或FAQCategoryID列并且我们希望对数字列(ViewCount)和日期/时间列(DateEntered)设定格式另外我们希望数字列的值居中这些均可通过几行易于阅读易于理解的代码完成
<asp:DataGrid runat=server id=dgPopularFAQs
BackColor=#eeeeee Width=%
HorizontalAlign=Center
FontName=Verdana CellPadding=
FontSize=pt AutoGenerateColumns=False>
<HeaderStyle BackColor=Black ForeColor=White
FontBold=True HorizontalAlign=Center />
<AlternatingItemStyle BackColor=White />
<Columns>
<asp:BoundColumn DataField=CatName HeaderText=Category Name/>
<asp:BoundColumn DataField=Description HeaderText=FAQ Description />
<asp:BoundColumn DataField=ViewCount DataFormatString={:####}
HeaderText=Views ItemStyleHorizontalAlign=Center />
<asp:BoundColumn DataField=SubmittedByName HeaderText=Author/>
<asp:BoundColumn DataField=SubmittedByEmail HeaderText=Authors Email/>
<asp:BoundColumn DataField=DateEntered HeaderText=Date Added
DataFormatString={:MMddyyyy}/>
</Columns>
</asp:datagrid>
实际运行结果如下
Category Name
FAQ Description
Views
Date Added
Getting Started
Where can I host my ASP Web site for free (similar to GeoCities or Tripod or any of the many other free Web site sites)?
…
ASPNET
How can I format numbers and date/times using ASPNET? For example I want to format a number as a currency
Databases Errors
I am using Access and getting a error (or a [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot open the file (unknown) error) when trying to open a connection! How can I fix this problem?
…
如上例所示上述代码指定了需要显示的特定列并且应用了特定的格式请注意DataFormateString看上去很有趣它的格式始终是{:format string}{: …}指定通过格式化字符串(由…指定的)来格式化第一个参数(第一个参数指由DataReader返回的那个特定列的值)在示例中我使用了格式化字符串####它在每个数字前加上一个逗号格式化字符串MMddyyyy指定通过月日和年的格式显示日期/时间字段
结论
花一些时间看一下第一个示例(见DataGrid Web控件深度历险())和现在的示例改进确实很大!请注意所有这些样式和用户界面的改进不需要写一行代码就可实现我们只是在Web控件的标记中设定了一些属性!事实上如果你正在使用类似Visual Studio Net的编辑器 你可通过点击一些按钮选中一些复选框选择列表框的一些项来设定格式化选项想象一下在传统ASP中实现同样效果需要编写的那些冗长代码那会使你爱上ASPNet如果你现在还没有的话
祝编程愉快!