ASPxGridView Cool Feature: Column Header Parsing

ASP.NET Team Blog
23 July 2007

Problem:

Do you have database column names that look like this?

image

As we all know, database column names are rarely created with the end-user in mind.

Cause:

As you probably know, the developer or dba generally base the name on the column definition. Therefore, the first name will probably called FirstName, last name = LastName, and so forth.

But would you really want to display these unformatted column names to your end-users?

Possible Solutions:

In most cases you'll want to correct the column names before the end-user ever sees them. You can solve this problem in a couple ways. However, they are not the most elegant solutions:

1) You can change your query to display the correct name. This requires you do extra work of rewriting each column name correctly, i.e.:

SELECT FirstName AS "First Name", LastName AS "Last Name" ...

2) Or you can change it in the grid. This too requires you to do extra work.

A Better Option:

Ideally, you want your grid to be smart enough to figure out that if the column is labeled, CustomerID, then "Customer ID" should be displayed.

ASPxGridView does this by default. See for yourself.

That Invoices query from above is now displayed correctly within the ASPxGridView:

image

Did you notice how the column names are already formatted for you?

You can save time by not having to re-code every column name.

Under Your Control:

You may not always want the smart parsing, so with the ASPxGridView you have the power to choose.

You can set the Caption property of the column within the columns collection of the ASPxGridView. Let's take a closer look:

Simply set AutoGenerateColumns to False and Refresh the Schema:

image

The Columns collection is now rebuilt using the column names that were specified in the query. You can still set the Caption property of any of the columns to your liking:

image

What if you wanted to use the smart parsing but still have custom names on some columns? No problem.

You can also use the Caption property together with the smart name parsing.

With AutoGenerateColumns set to True, simply assign the column name during runtime:

protected void ASPxGridView1_DataBound(object sender, EventArgs e)
{
    ASPxGridView1.Columns[0].Caption = "Vessel Name";
}

Best of both worlds:

image

Pretty cool, huh?

3 comment(s)
Oliver Sturm (DevExpress)
Oliver Sturm (DevExpress)

Pretty cool, yes! But XAF was there first! :-)

Seriously, XAF uses a similar algorithm in all kinds of places where a caption for an element is needed, but has not been configured explicitely. This is not restricted to ASP.NET, but it works across all supported UIs - okay, only Windows Forms and ASP.NET at this time. The captions might be used as labels for edit fields, column captions in grids, in the reporting system, in the layout configuration dialog...

Finally, column names aren't the only source XAF uses for this mechanism, but we also look at other sources like class names and enums. So if you make sure to use proper names for classes, properties and enums, XAF saves you a ****load of time in customization work.

If you're not familiar with XAF, have a look here - possibly it's the right thing for you!

http://www.devexpress.com/xaf

24 July, 2007
Crono
Crono

This is cool, but I kinda dislike the fact it is the default behavior. This isn't what a developper would expect when using the control for the first time.

26 July, 2007
Mehul Harry (DevExpress)
Mehul Harry (DevExpress)

Oliver,

Yes, XAF and ASPxPivotGrid had this feature before but very soon they will all be residing on one location anyway.

Crono,

While it may not be expected, it is a great time saving feature. Most devs like this feature as it saves time, coding and formatting concerns. Also, it can be easily bypassed.

26 July, 2007

Please login or register to post comments.