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

RadGridView Data Format

2 Answers 146 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Vladimir
Top achievements
Rank 1
Vladimir asked on 08 Mar 2010, 02:29 AM
Hi.
I checked all blogs and still cannot find out how I can format phone number in the GridView cell.
I have a string in the database like 4445556666. I want to show 444-555-6666.
I tried DataFormatString="{}{0:##-###-####}" and "{}{0:000-000-0000}", but nothing happened, in the grid I have 4445556666.
Who can help?
Thank you.

2 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 08 Mar 2010, 10:40 AM
Hi,

Generally such format string will work if your original value is integer - if you have string you will need to use IValueConverter instead:

XAML
<Window x:Class="WpfApplication1.Window1"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="Window1">
    <Grid>
        <Grid.Resources>
            <local:MyConverter x:Key="converter" />
        </Grid.Resources>
        <telerik:RadGridView ItemsSource="{Binding}" AutoGenerateColumns="False">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding ID}"/>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Phone, Converter={StaticResource converter}}" />
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</Window>

C#
using System;
using System.Linq;
using System.Windows;
using System.Windows.Data;
 
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
 
            DataContext = from i in Enumerable.Range(0, 100)
                          select new
                          {
                              ID = i,
                              Phone = "4445556666"
                          };
        }
    }
 
    public class MyConverter : IValueConverter
    {
        #region IValueConverter Members
 
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return System.Convert.ToInt64(value).ToString("##-###-####");
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
 
        #endregion
    }
 
}



All the best,
Vlad
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Vladimir
Top achievements
Rank 1
answered on 08 Mar 2010, 05:06 PM
Thank you, Vlad, it works fine.
I thought IValueConverter works only with numeric values.
Tags
GridView
Asked by
Vladimir
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Vladimir
Top achievements
Rank 1
Share this question
or