This is a migrated thread and some comments may be shown as answers.

RadGridViewWPF: format all decimal fields?

2 Answers 196 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dave
Top achievements
Rank 1
Dave asked on 27 Feb 2012, 10:57 PM
I have a RadGridView with auto column generation on, and the data source is a DataTable whose dimensions are unknown at compile time. I would like to make all decimal fields in the grid use money format, like "$#,##0.00".  Notice that I can easily set the FontFamily for text fields.  If only I could set a simple "NumericFormat=..." value !!

When the columns are unknown I cannot write XAML to declare nor to format each column. I could use C# code-behind, but what event shall I catch, and which method sets the numeric formatting at the right time?  Thanks.

 

<telerik:RadGridView Margin="0,22,0,0" Name="radGridView1" DataLoadMode="Asynchronous" FontFamily="Century Gothic" FontSize="16" AutoExpandGroups="True" AutoGenerateColumns="True" >

 

 </telerik:RadGridView>

2 Answers, 1 is accepted

Sort by
0
Accepted
Dimitrina
Telerik team
answered on 28 Feb 2012, 07:44 AM
Hi,

 You can subscribe for the AutoGeneratingColumn event of the RadGridView. It occurs every time a new column is auto generated by the grid view control. That way you can change the properties of the generated column.

Greetings,
Didie
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Dave
Top achievements
Rank 1
answered on 28 Feb 2012, 07:44 PM
Thanks.  That event works quite nicely.  For future reference, I used this:
private void radGridView1_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e) {
  GridViewDataColumn column = e.Column as GridViewDataColumn;
  // set the cell format of numeric values.
  if (column.DataType.Name != "String") {
    column.DataFormatString = "{0:C0}";
    column.TextAlignment = TextAlignment.Right;
  }
}

Most of this is not documented in one place. The data format string is specified by Microsoft via msdn.microsoft.com.
Tags
GridView
Asked by
Dave
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Dave
Top achievements
Rank 1
Share this question
or