Display an Image when the GridView has no data

I recently answered a question on the forum where a user wanted to display an image when the GridView returns empty data. Now you must be already be familiar with the GridView’s EmptyDataText property which displays a message to the user when there are no results returned from the GridView control’s data source.

Now instead of a text message, if you need to display an image or any HTML/ASP.NET control, you can use the EmptyDataTemplate. In this snippet below, we are using the image control in the <EmptyDataTemplate> to display an image.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
ShowFooter="true" AllowPaging="True" AllowSorting="True"
>

<
EmptyDataTemplate>
<
asp:Image id="imgOne"
ImageUrl="~/images/Emptydata.jpg"
AlternateText="No results"
runat="server"/>
</
EmptyDataTemplate>


Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

1 comment:

Ramesh said...

Display an Image when the GridView has no data

That was a good post, but I wanted it on other way. Suppose we are not using sqldatasource control and binding a datatable to it. If datatable doesn't have any rows, the grid is not display neither the EmptyDataText.

I appreciate any solution for this