views:

260

answers:

6

I have a basic GridView that displays a list of tasks to do (just an example)

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AllowSorting="True" >
    <Columns>
        <asp:BoundField DataField="todo" HeaderText="To Do" ReadOnly="True" SortExpression="todo" />
        <asp:BoundField DataField="byDate" HeaderText="By When" ReadOnly="True"

SortExpression="byDate" />

the data source is specified within the aspx page and it is a result set from a stored procedure

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="..."
      SelectCommand="pToDoList" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>

So the rendered page presents tasks as rows of data (a row per task)

My question is

When there is not data returned by stored procedure the page remains blank. I would like to have a text instead saying for example: "nothing to do today"

How to do that? Thank you

+1  A: 

Add a Label to the page, call it ErrorLabel, then do something like this in the DataBound event of the GridView:

protected void GridView1_DataBound(object sender, EventArgs e)
{
    if(GridView1.Rows.Count <= 0) ErrorLabel.Text = "Nothing to do today";
    else ErrorLabel.Visible = false;
}

Although a Google search would have probably gotten that for you.

Matthew Jones
+1 Thanks Matthew, I will keep that option in mind
padn
A: 

One option is to use the Footer to display this message. During the ItemDataBound, if the ItemType is the footer, check if the DataSource is empty and if it is, display the message.

Or if you want to work declaritvely in the ASPX you can use the EmptyDataTemplate and add your message there.

kay.herzam
+5  A: 

Even simpler, use the EmptyDataTemplate: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.emptydatatemplate.aspx

MikeB
Should not have included "Even simpler". Keep forgetting that answers get re-ordered around these here parts :-/
MikeB
+3  A: 

there is a property "EmptyDataText" that allows you to specify text if the gridview is empty

Pharabus
+1 thanks that will actually work fine in my case. I accepted the template suggestion as it give more flexibility with formatting
padn
A: 

I would simply would add a label control noDataLabel containing the text "Nothing to do today", and add some code in the Page_Load event:

if (GridView1.Rows == 0)
{
    GridView1.Visible = false;
}
else
{
    noDataLabel.Visible = false;
}

It is a bit crude, but it beats have embedded C# inside your webform HTML. If there is no data, and the form is rendered, the GridView is simply ignored.

Mark Bertenshaw
A: 

Dears i was writing in my blog about this issue but for c# , you can review the post and check if it will be helpful or not ,

http://blog.waleedmohamed.net/2009/04/show-grid-view-header-and-footer-when.html

i was using a generic way with an helper to retrieve an empty list to fill the gird please check and if u have any quest don't hesitate to contact me or comment on stack site

Waleed Mohamed