C# 没有记录时如何在gridview“No Records Found”中显示文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14132464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display a text in gridview "No Records Found" when there is no record
提问by Chow.Net
I have iframe where I have several charts and gridviews where the charts and gridviews are fetching data from database.when records found no issue but for some users don't have records for one of gridviews where I have to display a message "No records found".
我有 iframe,我有几个图表和网格视图,其中图表和网格视图从数据库中获取数据。当记录发现没有问题但对于某些用户没有网格视图之一的记录时,我必须显示一条消息“未找到记录”。
kindly suggest me on this. I tried the below code which displays a message but it displays in small box,but I need to display inside the gridview.
请就此向我提出建议。我尝试了下面的代码,它显示一条消息,但它显示在小框中,但我需要在 gridview 中显示。
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EmptyDataTemplate>No records Found</EmptyDataTemplate>
Here I am not showing grid header,but i have to!
在这里我没有显示网格标题,但我必须!
采纳答案by lahsrah
Set ShowHeaderWhenEmpty
property on the GridView to true
.
将ShowHeaderWhenEmpty
GridView 上的属性设置为true
.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.showheaderwhenempty.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.showheaderwhenempty.aspx
But you still have to DataBind the gridview. This code snippet worked for me:
但是您仍然必须对 gridview 进行数据绑定。此代码片段对我有用:
<asp:GridView ID="GridView1" runat="server" ShowHeaderWhenEmpty="True" EmptyDataText="No records Found">
<Columns>
<asp:boundfield datafield="CustomerID" headertext="Customer ID"/>
<asp:boundfield datafield="Name" headertext="Name"/>
</Columns>
</asp:GridView>
回答by COLD TOLD
try setting emptydatatext
and ShowHeaderWhenEmpty
尝试设置 emptydatatext
和 ShowHeaderWhenEmpty
<asp:gridview id="GridView"
datasourceid="DataSource"
autogeneratecolumns="true"
emptydatatext="No data in the data source."
runat="server"
ShowHeaderWhenEmpty="True">
</asp:gridview>
回答by ashish agrawal
The approach I am using for gridview for no records found is that,when there are no records in database then I clear all the rows in grid and add a new row in the grid as a text no records found,or you can create a function for no result found and then use that in bind function of gridview. When there are no records you can call that function.
我用于 gridview 的方法没有找到记录是,当数据库中没有记录时,我清除网格中的所有行并在网格中添加一个新行作为文本未找到记录,或者您可以创建一个函数没有找到结果,然后在gridview的绑定函数中使用它。当没有记录时,您可以调用该函数。
回答by Fandango68
For those still using ASP.net 2 or 3.5, the ShowHeaderWhenEmpty
property does not exist. To get around this, just simply use the EmptyDataText="..."
property and make the gridview visible only when data was found (in code behind).
对于仍在使用 ASP.net 2 或 3.5 的用户,该ShowHeaderWhenEmpty
属性不存在。要解决这个问题,只需简单地使用该EmptyDataText="..."
属性并使 gridview 仅在找到数据时才可见(在后面的代码中)。