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

How to format autogenerated columns?

2 Answers 245 Views
GridView
This is a migrated thread and some comments may be shown as answers.
SPE
Top achievements
Rank 1
SPE asked on 16 Mar 2011, 05:53 AM
Hello.
How can I apply FormatString to Autogenerated columns?
I am using Silverlight DataTable from Telerik blog as an ItemsSource for RadGridView, and columns are autogenerated.
I'd like to format the autogenerated columns based on its data type.

I've found an example of doing this for Silverlight DataGrid, as below.
But as RadGridView's GridViewAutoGeneratingColumnEventArgs class doesn't have PropertyType property, I can't use this example.
void gridView_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyType == typeof(DateTime))
    {
         DataGridBoundColumn obj = e.Column as DataGridBoundColumn;
         if (obj != null && obj.Binding != null)
              obj.Binding.StringFormat = "{0:d}";
    }
}
Thank you for your support.

2 Answers, 1 is accepted

Sort by
0
Accepted
Maya
Telerik team
answered on 16 Mar 2011, 09:04 AM
Hello sunmin,

You may set the string format as follows:

private void RadGridView1_AutoGeneratingColumn(object sender, Telerik.Windows.Controls.GridViewAutoGeneratingColumnEventArgs e)
        {
            GridViewDataColumn column = e.Column as GridViewDataColumn;
            if (column.DataType == typeof(int))
            {
                column.DataFormatString = "{0:p}";                             
            }
        }


Kind regards,
Maya
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
SPE
Top achievements
Rank 1
answered on 17 Mar 2011, 02:25 AM
It works quite nicely. thanks!
Tags
GridView
Asked by
SPE
Top achievements
Rank 1
Answers by
Maya
Telerik team
SPE
Top achievements
Rank 1
Share this question
or