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 and so on. 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


About the Author

Nikolay Diyanov

is the Product Manager of Telerik's UI for iOS and UI for Xamarin divisions. He joined the company back in 2007 as a Support Officer and made his way up the ladder over the past few years. Delivering outstanding solutions that make developers lives easier is his passion and the biggest reward in his work. In his spare time, Nikolay enjoys travelling around the world, hiking, sun-bathing and kite-surfing.

@n_diyanov

Related Posts

Comments