Fixing the GridView CSS Adapter lack of Empty data rendering

October 10 2010    css

Don't ask me why.  No, please don't!  I'm playing with the CSSFriendly adapters for ASP.NET for some more CSS friendly rending of Grid Views and I just found it's not working with EmptyDataTemplates.  A quick google revealed a bunch of devs complaining about it in the forums and declaring to not use CSS Adapters.  I've just invested a few hours in styling my Grid Views (and using skinning and themes. Again, don't ask :p) and I really didn't want to spend another few hours or more getting CSS to play nice with the original GridView rendering.

"What the hell, I'm a developer.  I'll just develop me a solution" :)  15 minutes later I have this:

using System.Web.UI.WebControls;
using CSSFriendly;

namespace TurrbalTerror
    public class SSGridViewAdapter : GridViewAdapter
        protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
            GridView control = base.Control as GridView;
            if (control != null) {
                if (control.Rows.Count > 0) {
                else if (control.DataSource != null) {
                    var panel = new MessagePanel();
                    panel.MessageType = "Information";
                    panel.Message = !string.IsNullOrEmpty(control.EmptyDataText)
                                        ? control.EmptyDataText
                                        : "Contains no data";


We want to make sure that the grid doesn't have any rows because it doesn't have a data source yet, so we need to render the rows if the rows exist and then if the rows don't exist and the grid has a data source, we can display the empty data text.  All that is required is that you set the EmptyDataText property on the GridView to the text you want to display when no rows are found.  MessagePanel is a CustomControl I've previously written to display and format a variety of messages to the user.

It's not a true fix because it doesn't consider whats in the EmptyDataTemplate. But for my needs it is "fixed" :)

Post a comment

comments powered by Disqus