I have a list of objects populated from a database. I need to display an error message if the list is empty and display a grid view otherwise.
How do I check if a List<T>
is empty in C#?
I have a list of objects populated from a database. I need to display an error message if the list is empty and display a grid view otherwise.
How do I check if a List<T>
is empty in C#?
You can make your datagrid visible false and make it visible on the else section.
What about using the Count() method.
If the list implementation you're using is
IEnumerable<T>
and Linq is an option, you can useAny
:Otherwise you generally have a
Length
orCount
property on arrays and collection types respectively.You should use a simple
IF
statementgridview itself has a method that checks if the datasource you are binding it to is empty, it lets you display something else.
If you're using a gridview then use the empty data template: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.emptydatatemplate.aspx