如何检查 Gridview 是否为空
我有一个 ASP.NET 2.0 (C#) Web 应用程序,其中有一个从 Oracle 数据库获取数据的 gridview。
我想知道如何检查 gridview 是否为空,并执行某些操作。
我已经尝试过:
if(GridView.Rows.Count == 0)
{
// Do Something
}
但它不起作用......
有什么想法吗?
谢谢。
I have an ASP.NET 2.0 (C#) web app, and in it I have a gridview that gets its data from an oracle database.
I want to know how to check if the gridview is empty, and the do something.
I have already tried:
if(GridView.Rows.Count == 0)
{
// Do Something
}
but it doesn't work...
Any ideas?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您的代码应该可以工作。 但只有在调用 GridView.DataBind() 之后。 一般来说,我不检查 GridView 本身,而是检查网格视图的数据源。
Your code should work. But only after GridView.DataBind() has been called. Generally I don't check the GridView it's self, but the datasource of the grid view.
这是行不通的,因为 GridView 是数据绑定的,并且稍后会在渲染页面时获取实际数据。 您应该通过直接查询网格视图的数据绑定源来检查这一点(查看网格视图绑定的实际列表是否为空)。
如果您只想在空时显示某些内容,则应在标记中使用
:This doesn't work since the
GridView
is data bound and is going to fetch the actual data at a later time while rendering the page. You should check this by directly querying the data binding source of the gridview (see if the actual list that's grid view bound to is empty or not).If you just want to display something when it's empty, you should use
<EmptyDataTemplate>
in your markup:我同意其他回复。 我想添加一点信息,您应该在 databind 方法之后获得 rows.count :
I agree with the other responses. I want to add little information, you should get rows.count after databind method :
如果您使用数据绑定,则数据源的行计数而不是网格本身的计数。
If you're using databinding, the the row count of the datasource not the count on the grid itself.
它非常简单:希望它适合您..:)
使用 GridView DataBound 事件:该事件在数据绑定后触发。
Its very easy: Hope it works for you.. :)
Use event of GridView DataBound: which fires after data is bound.
首先创建一个辅助类。
然后只需调用 IsEmpty
First create a helper class.
Then just call IsEmpty
如果您已将 GV 配置为自动从数据库获取数据,那么您可以添加以下行作为源模式下 GV 的第一条语句:
然后继续使用 GV 中的正常代码。
In case you've configured your GV to automatically fetch the data from DB, then you may add the following line as the first statement of your GV in Source mode:
And then continue with the normal code in your GV.
根据已经的答案, GridView.Rows.Count 本身是不够的,具体取决于 GridView 的性质,特别是如果它是动态 gv,在大多数情况下确实如此,而且您还必须考虑分页、页眉和页脚,它们会改变行数。
我用一个简单的方法来告诉我......
所以运行这样的东西会告诉你行是否真的“
“DATA”行,或者为空,或者什么都没有!
显然,在我的例子中,我有一个 HiddenField 来告诉我 GridViewRow 是否是实际的数据行,因为我用空白行(出于我的目的)和一些数据行预填充了我的 gridview。
但是,基于 DataRow 与 HeaderRow 等检查的更简单版本...
我希望这会有所帮助。
简而言之,不幸的是,没有 GridView.IsEmpty() 函数,除非您如下所示枚举一个函数。
Based on answers already,
GridView.Rows.Count
is not enough on its own, depending on the nature of yourGridView
, especially if it's a dynamic gv, which in most cases it is, plus you have to factor inPaginating
, Headers and Footers, which alter the row count.I use a simple method to tell me ...
So running something like this will tell you if the Rows are really "
"DATA" rows, or empty or nothing!
Obviously in my case I have a HiddenField to tell me if the GridViewRow is an actual data row, as I prefill my gridview with blank rows (for my purposes) and some datarows.
However, a simpler version to check based on DataRow vs HeaderRow, etc...
I hope this helps.
In short, there is no GridView.IsEmpty() function unfortunately, unless you enumerate one as shown below.