Quickly formatting data with the RadGridView for WinForms

Thursday, January 29, 2009 by WinForms Team | Comments 1

Most developers have encountered a need at some point to modify how data is displayed to the user.  The RadGridView offers a very straightforward way to format data without having to modify the source data.  For example, you may not want the time displayed with a DateTime field, or you want to change the structure of the data to meet an internal standard.  The screenshot below shows three different fields which display the default DateTime format.

image  

This may be valid data, but it may not be useful to the user and we could potentially save screen real estate by eliminating unnecessary information.  By using the FormatString property of the column you can apply the same standard formatting options available with the string.Format method.

radGridView1.Columns["OrderDate"].FormatString = "{0:dddd, MMM dd, yyyy}";  
radGridView1.Columns["RequiredDate"].FormatString = "{0:yyyy/MM/dd}";  
radGridView1.Columns["ShippedDate"].FormatString = "{0:dd - MM - yyyy}";  

The code shown will apply different custom formatting options, as seen below.  The time data is no longer visible since we are specifically concerned with the date.

image

Formatting can is particularly useful when dealing with numeric data since it can represent currency, percentages, etc...  Look at the Order Details table information below.  The values are informative, but you could not know that 14.0000 is a dollar amount necessarily unless you looked at the column header.

image

By applying formatting we can make it much more readable for the user and give the visual hint of the type of information the user is looking at.

this.radGridView1.Columns["UnitPrice"].FormatString = "{0:$#,###0.00;($#,###0.00);0}";  
this.radGridView1.Columns["Quantity"].FormatString = "{0:#,###00 units}";  
this.radGridView1.Columns["Discount"].FormatString = "{0:#0%;(#0%);none}";  

The code above using the same formatting technique will allow the data to be represented more accurately. 

image

The simplicity of this feature makes it something you can implement with your existing or new applications quickly.  To assist you in getting started I have outlined some handy references for the formatting options available.

DateTime Format Options  http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx

Number Format Options http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.aspx

1 Comments

  • Raymond 17 Feb 2010
    Is it possible to sort the formatted version of the grids data? For example I have a list of Dates that I am formatting with {0:ddd}.  When I click on the column headers it will sort by the actual underlying Date, not Mon/Tues/Wed etc. I want to be able to group the Days together.

    Thanks in advance.

    -Raymond

Add comment

  1. Formatting options
       
     
     
     
     
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)