检查列表是在C#空 [英] Check if list is empty in C#

查看:159
本文介绍了检查列表是在C#空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库填充对象的列表。

I have a list of objects populated from the database.

我需要的,如果该列表是空的,以显示一条错误消息,否则显示网格视图。

I need to display an error message if the list is empty and display the grid view otherwise.

推荐答案

现在的问题是不是真的清楚。为什么不...

The question is not really clear. Why not...

bool isEmpty = !list.Any();
if(isEmpty)
{
    // error message
}
else
{
    // show grid
}



GridView控件也已经一个的 EmptyDataTemplate 。这显示,如果该数据源是空的。

The GridView has also an EmptyDataTemplate which is shown if the datasource is empty.

<emptydatarowstyle backcolor="LightBlue" forecolor="Red"/>

<emptydatatemplate>

  <asp:image id="NoDataErrorImg"
    imageurl="~/images/NoDataError.jpg" runat="server"/>

    No Data Found!  

</emptydatatemplate> 

这篇关于检查列表是在C#空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆