Answered Phone Number in Datagrid

  • Tuesday, May 01, 2012 11:08 AM
     
      Has Code
    Hi,

    im displaying Phone Number in a Datagrid
    <DataGridTextColumn Header="PHONE" Binding="{Binding Path=Phone}"/>
    all the numbers are showing as EX:123456789 from the DB
    but i would like to show them in US format...
    can anyone help me...

All Replies

  • Tuesday, May 01, 2012 12:26 PM
     
     Proposed Answer

    Hi Issac,

    U can make use of "StringFormat" attribute which is available in WPF Bindings.

    Please have a look at the link below explaining the same.

    http://elegantcode.com/2009/04/07/wpf-stringformat-in-xaml-with-the-stringformat-attribute/

    http://blogs.msdn.com/b/vsdata/archive/2009/07/07/customize-format-of-datetime-string-in-wpf-and-winform-data-binding.aspx

    Hope it helps!

    Please mark it as an answer if it resolves ur issue.


    Regards, Parth Shah

    • Proposed As Answer by parth.shah Tuesday, May 01, 2012 12:26 PM
    •  
  • Tuesday, May 01, 2012 4:17 PM
     
     Answered Has Code

    Hello Issac.

    I actually use a converter for myprojects.

    using System;
    using System.Windows.Data;
    
    namespace WpfApplication1
    {
    	public class PhoneConverter : IValueConverter
    	{
    		#region IValueConverter Members
    
    		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    		{
    			double a = System.Convert.ToInt64(value);
    			return a.ToString("(###) ###-####");
    		}
    
    		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    		{
    			throw new NotImplementedException();
    		}
    
    		#endregion
    	}
    }

    And the xaml...

    	<Window.Resources>
    		<local:PhoneConverter x:Key="PhoneConverter"/>
    	</Window.Resources>
    
    	<Grid x:Name="LayoutRoot">
    		<TextBox x:Name="textBox" HorizontalAlignment="Left" TextWrapping="Wrap" Text="5555555555" VerticalAlignment="Top" Margin="8,8,0,0" Width="104.193"/>
    		<TextBlock x:Name="textBlock" Margin="8,58,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Text="{Binding Text, Converter={StaticResource PhoneConverter}, ElementName=textBox}" Width="104.193"/>
    	</Grid>

    This thread also addressed it or offered a solution... http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/476c28ad-fbcf-44f4-95c1-9b5deaa0742d

    But I could never get the string FormatString to work properly, only the int32, which should be 64 for a US number, StringFormat.

    ~Christine


    My Gallery


    • Edited by Christine L. _ Tuesday, May 01, 2012 4:18 PM
    • Marked As Answer by Issac999 Wednesday, May 02, 2012 8:17 AM
    •  
  • Wednesday, May 02, 2012 5:31 AM
     
     

    Hi Issac,

    The method suggested by Christine is also correct.

    U can either make use of StringFormat attribute in WPF binding or u can also make use of Converter class as well.

    Feel free to ask if u have any further queries.


    Regards, Parth Shah

  • Wednesday, May 02, 2012 8:18 AM
     
     
    thank u....worked perfectly for me......