方法一:采用GridView中的EmptyTemplate来实现,模版中写一个静态的table;
下面贴出aspx代码:
<head runat="server">
<title>GridView使用EmptyTemplate来使无数据时显示表头</title>
</head>
<body>
<form id="form1" runat="server">
<strong>GridView使用EmptyTemplate来使无数据时显示表头</strong>
<div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="AccessDataSource1" BackColor="White" BorderColor="#336666" Width="600px"
BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal">
<FooterStyle BackColor="White" ForeColor="#333333" />
<RowStyle BackColor="White" ForeColor="#333333" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
<EmptyDataTemplate>
<table cellpadding="0" cellspacing="0" width="600px">
<tr>
<td width="100px">
用户编号
</td>
<td width="100px">
用户姓名
</td>
<td width="100px">
用户类型
</td>
<td width="100px">
用户年龄
</td>
<td width="100px">
电子邮件
</td>
<td width="100px">
联系地址
</td>
</tr>
<tr>
<td colspan="6" align="center">
没有数据显示
</td>
</tr>
</table>
</EmptyDataTemplate>
</asp:GridView>
</div>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/GridViewTest.mdb"
SelectCommand="SELECT [UuserID], [UuserName], [UuserPower], [Age], [Email], [Address] FROM [Uuser]">
</asp:AccessDataSource>
</form>