5

I can't seem to get either EmptyDataTemplate or EmptyDataText of a GridView to work.

I'm fetching the GridView contents in de codebehind and attaching them with using DataBind(). I've tried having them as null and as an empty List, and in both cases the text I put into EmptyDataTemplate or EmptyDataText is not displayed.

What am I doing wrong?

EDIT (Code snippet)

This is my GridView:

<asp:GridView ID="grid" runat="server" EmptyDataText="EMPTY">
</asp:GridView>

And I've tried these two for binding the data:

grid.DataSource = new List<object>();
grid.DataBind();

grid.DataSource = null;
grid.DataBind();
Farinha
  • 17,636
  • 21
  • 64
  • 80

5 Answers5

5

This problem is caused by using the so-called CSS-Friendly Control Adapters. With them enabled (and they seem to be enabled by default), EmptyDataTemplate and EmptyDataText don't work as expected.

To disable the adapters, go to the App_Browsers folder, and in the CSSFriendlyAdapters.browser file, comment out the following section (or the section related to the control you're using):

<adapter controlType="System.Web.UI.WebControls.GridView"
               adapterType="CSSFriendly.GridViewAdapter" />

The big problem is the styles will go away.

Farinha
  • 17,636
  • 21
  • 64
  • 80
  • hi Farinha, I added related question http://stackoverflow.com/questions/3856890/gridview-using-css-friendly-control-adapters-removes-emptydatatemplate-and-empt - hopefully there is an alternative solution that would allow keep the styles – kristof Oct 04 '10 at 15:57
  • Thanks, that at least explains things. – Farinha Oct 05 '10 at 12:07
  • Thank you man, although you didn't help me directly, you saved me a lot of time, giving me a clue of the problem was tackling.. Thanks once again! – Saxophonist Mar 10 '15 at 18:22
4

I ran into a similar problem and noticed I had logic around my DataBind to ignore the databinding method if my datasource was empty.

p.campbell
  • 98,673
  • 67
  • 256
  • 322
TreK
  • 1,144
  • 2
  • 13
  • 25
2

The EmptyDataTemplate will override any EmptyDataText, so it could be that you've defined the template incorrectly somehow, which is keeping you from seeing the EmptyDataText.

If you're binding an empty list to the GridView, it should display the EmptyDataText, so long as you don't have a conflicting EmptyDataTemplate, e.g.

<%-- GridTest.ascx --%>
<asp:GridView ID="Grid" runat="server" EmptyDataText="No Data!" />

// GridTest.ascx.cs
Grid.DataSource = new List<object>();
Grid.DataBind();

Are you doing something different than this?

bdukes
  • 152,002
  • 23
  • 148
  • 175
  • That's what I have (edited the question with code snippets). And instead of the GridView I just get blank. No sign of the EmptyDataText. – Farinha Jun 03 '10 at 14:31
  • @Farinha When you bind a list with some contents, the GridView renders? I'm wondering if maybe your data-binding code isn't even running... – bdukes Jun 04 '10 at 19:04
  • Yes, it renders OK when there's data in it. – Farinha Jun 08 '10 at 09:29
1

Can User Empty Data Template.....

    <Columns>
             ............
             ............
             ............
    <Columns>
<EmptyDataTemplate>
        <asp:Label ID="lblEmptyTxt" runat="server" Text="No Data"></asp:Label>
      </EmptyDataTemplate>
                        </asp:GridView>
BJ Patel
  • 6,148
  • 11
  • 47
  • 81
0

And don't do what I did - The GridView ForeColor property wasn't set and it defaults to white which was also my BackColor property!

IrishChieftain
  • 15,108
  • 7
  • 50
  • 91