"no rows" message in a datagrid

  • Thread starter Thread starter Andy Fish
  • Start date Start date
A

Andy Fish

Hi,

when there are no rows to display in my asp.net datagrid control, I'd like
to put in a message saying "No results" or some such

Ideally I'd like the message to be centred across all the columns, but I'd
settle for the message appearing in a single cell, or header/footer line.
maybe there's a way to make the whole control invisible when there are no
rows in the data source.

TIA for any ideas

Andy
 
Any control can be made invisible by setting its "Visible" property to
false. You can check whether your data source has any rows and set the
DataGrid's "visible" property to false if it does. For example:

ASP.Net:

<asp:Label id="lblNoRows" runat="server" visible="false">No
Results</asp:Label><br>
<asp:DataGrid id="dgData" runat="server" />

C# Code Behind:

private void Page_Load(object sender, System.EventArgs e)
{
DataTable dtSource;
//Code here retrieves data from data source into dtSource

if (dtSource.Rows.Count == 0)
lblNoRows.Visible = true;
else
{
dgData.DataSource = dtSource;
dgData.DataBind();
}
}
 
Do'h!

thanks ben,

I was thinking I had to somehow do this inside the databind - I was
forgetting I can just check the count of rows before calling DataBind()

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top