ASPxGridView Cool Feature: Column Header Parsing

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?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s