GridView - 在空数据源上显示标题 [英] GridView - Show headers on empty data source
问题描述
在 C# 中,即使数据源为空,我如何仍然显示 gridview 的标题.
In C# how do I still show the headers of a gridview, even with the data source is empty.
我不会自动生成列,因为它们都是预定义的.
I am not auto generating the columns as they are all predefined.
目前我正在做的是以下.
Currently what I am doing is the following.
从存储过程中取回一个DataTable,然后设置gridview的DataSource,然后调用DataBind().
Get a DataTable back from a stored procedure, then set the DataSource of the gridview, and then call DataBind().
当我有数据时这很好用,但是当没有行返回时,我只会在网格应该出现的地方得到一个空白点.
This works fine when I have data, but when no rows are returned then I just get a blank spot where the grid should be.
感谢所有 .NET 4+ 属性.我在 .NET 3.5 天问过这个问题.这现在容易多了.:)
推荐答案
ASP.Net 4.0 添加了布尔型 ShowHeaderWhenEmpty
属性.
ASP.Net 4.0 added the boolean ShowHeaderWhenEmpty
property.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.showheaderwhenempty.aspx
<asp:GridView runat="server" ID="GridView1" ShowHeaderWhenEmpty="true" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField="LastName" />
</Columns>
</asp:GridView>
注意:除非使用非空值调用 DataBind(),否则不会出现标题.
Note: the headers will not appear unless DataBind() is called with something other than null.
GridView1.DataSource = New List(Of String)
GridView1.DataBind()
这篇关于GridView - 在空数据源上显示标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!