您的位置:首页 > 其它

GridView数据源为空时显示表头方法

2012-11-09 21:00 393 查看
通常我们用Gridview绑定数据时会先判断数据源是否为空,如果为空则不绑定,那么此时页面将不会显示表格,如果希望在没有数据的情况下依然显示表头,该如何实现呢?

EmptyDataTemplate——空数据模板是GridView控件的属性之一。EmptyDataTemplate表示当记录为空时显示的定义内容。它用来获取或设置在GridView 控件绑定到不包含任何记录的数据源时,在GridView.控件上所显示的空数据行的内容,这个内容可以根据需要来设置。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Width="200px"
BorderWidth="0" BackColor="#DDBBDD">
<EmptyDataTemplate>
<table cellspacing="0" style="border-collapse: collapse; width: 100%; background-color: red;
margin: -1px">
<tr>
<th>
序号
</th>
<th>
姓名
</th>
<th>
年龄
</th>
</tr>
</table>
</EmptyDataTemplate>
<Columns>
<asp:BoundField HeaderText="序号" DataField="ID" />
<asp:BoundField HeaderText="姓名" DataField="Name" />
<asp:BoundField HeaderText="年龄" DataField="Age" />
</Columns>
</asp:GridView>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: