Skip navigation.

Authentication Ticket HurdleAll recent postsLightweight Database Cache Dependencies: Part I

Sorting DataView on Non-existent Columns

The DataView class represents a databindable, customized view of a DataTable for sorting, filtering, searching, editing, and navigation. It's an awfully helpful class... except when it comes to sorting the underlying data source on a non-existent column.

An example might help illustrate my point. Suppose, you want to display all users of your system in a nicely formatted DataGrid. The way in which you obtain your user list (database, web service, XML file) is irrelevant to this discussion.

DataSet users = new DataSet ();
DataTable table = new DataTable ("Users");

table.Columns.Add ("PKId", typeof (int));
table.Columns.Add ("Name", typeof (string));
users.Tables.Add (table);

// Add users from a database, for example.
DataView dv = new DataView (users.Tables[0]);
dv.Sort = "Name";

In this sample I create and initialize a dataset by hand. Once it's ready, I create a DataView on the only table ("Users") and sort it by the "Name" column. At this point you may bind the data view to a grid/repeater/list/etc.

What happens if you rename the "Name" column into "FullName"?

table.Columns.Add ("FullName", typeof (string));

The Sort property will blow up with an exception. The exception says the column "Name" was not found (remember, we renamed it?)

What's the biggie? It functions as designed, you might say. Apparently, it does, but it's not developer friendly. It's way too easy to overlook this scenario in a real-life app (which is exactly what happened to me).

How is it possible to mess up sorting column names? It just is. Trust me.

Let's look at another extreme:

dv.Sort = null;

Hmmm, this works fine. The DataView class has a special check for this:

public void set_Sort (string value)
{
 if (value == null)
  value = "";
 ...
}

Why not blow up, too? null is not a valid column name.

I don't know what's up with this, but I don't appreciate it. Setting try..catch blocks around .Sort all over the code doesn't sound exciting. Maybe developing a "friendly" dataview derivative is a way out.

class FriendlyDataView : DataView {
 public FriendlyDataView (DataTable table) : base (table) {}

 public new string Sort
 { 
    get { return base.Sort; }

    set { 
       try { 
            base.Sort = value; 
       }
       catch { 
            base.Sort = null; 
       }
    }
 }
}

This seems like an overkill. Any other ideas?

Comments

Comment permalink 1 James |
"null" maybe a valid column name but a null string is just a null string.

Emails and Notifications

Would you like to be notified when somebody responds to this post?  Would you like to have these comments emailed to you?

TrackBacks

Sorry, TrackBacks are not allowed.

Submit your comment

Please enter only text since all HTML tags except hyperlinks will be stripped. Hyperlinks will become live links. Any comments with flaming or offensive language will be deleted. Be courteous to other posters. Thank you.

Your name (required):
Your email (optional):
Your site's URL (optional):
Enter this number
Type in the number above:
Comment (required):