检查列表是在C#空

检查列表是在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>