【发布时间】:2010-07-17 01:22:51
【问题描述】:
我只是添加了网格视图并添加了列并给出了 headertext 但是当我运行应用程序时,我看不到任何网格,至少我应该看到网格列名称
我还需要做些什么吗
【问题讨论】:
-
如果对您有用,别忘了将答案标记为已接受...
我只是添加了网格视图并添加了列并给出了 headertext 但是当我运行应用程序时,我看不到任何网格,至少我应该看到网格列名称
我还需要做些什么吗
【问题讨论】:
确认您已正确连接所有内容并分配DataSource 并执行DataBind()。一旦您确认这两件事正在发生,请确保您的 DataSource 正在返回某种类型的结果集,其中至少包含一项。
除非结果集中至少有一项,否则GridView 将不会显示任何内容。如果您绑定到DataSet 或某种类型的对象列表并且其中没有项目,则网格将根本不显示。甚至没有标题。在这种情况下,您应该设置 EmptyDataText 属性来显示某些内容。
如果这有帮助,请发布您的 GridView 标记和绑定网格的代码,我会看看是否能找出问题所在。
【讨论】:
检查aspx页面代码
<asp:MyGridView runat="server" DataKeyNames="pkey" AutoUpdateAfterCallBack="true"
Width="100%"
ID="grduser" AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="Sr No." DataField="rownumber" ReadOnly="true" HeaderStyle-Width="10px"
ItemStyle-Width="10px" />
<asp:BoundField HeaderText="FirstName" DataField="FirstName" SortExpression="FirstName"
ReadOnly="true" HeaderStyle-Width="120px" ItemStyle-Width="120px" />
<asp:BoundField HeaderText="LoginName" DataField="LoginName" SortExpression="LoginName"
ReadOnly="true" HeaderStyle-Width="120px" ItemStyle-Width="120px" />
<asp:BoundField HeaderText="Email" DataField="Email" SortExpression="Email" ReadOnly="true"
HeaderStyle-Width="120px" ItemStyle-Width="120px" />
<asp:BoundField HeaderText="Role" DataField="Role" SortExpression="Role" ReadOnly="true"
HeaderStyle-Width="30px" ItemStyle-Width="30px" />
<asp:BoundField HeaderText="Reportingto" DataField="Reportingto" SortExpression="Reportingto"
ReadOnly="true" HeaderStyle-Width="120px" ItemStyle-Width="120px" />
<asp:BoundField HeaderText="MobileNo" DataField="MobileNo" SortExpression="Mobile_no"
ReadOnly="true" HeaderStyle-Width="30px" ItemStyle-Width="30px" />
</Columns>
</asp:MyGridView>
绑定网格的cs文件代码
DataSet ds = new DataSet();
ds = //get dataset form the database
DataView dv = new DataView(ds.Tables[0]);
this.grduser.DataSource = dv;
this.grdusers.DataBind();
【讨论】:
正如凯尔西所说,最简单的方法是:
<emptydatatemplate>
No Data Found.
</emptydatatemplate>
其他技巧:
1) 覆盖 CreateChildControls(例如:http://forums.asp.net/t/1003306.aspx)
2) 手动插入一行(例如:http://geekswithblogs.net/dotNETvinz/archive/2009/03/11/tiptrick-show-header-and-footer-of-gridview-when-no-data.aspx)
【讨论】: