views:

337

answers:

2

I have a Silverlight DataGrid that's being populated with different types of data for each column. I'm trying to figure out how to format some of the contents of the DataGrid's cells, specifically for dates and formatting.

I have a date column that's currently displaying like: 3/11/2010 12:00:00 AM. I would rather it display like 3/14/2010.

I have a number column that's currently displaying like: 51.32. I'd rather it display as currency like $51.32.

I'm not sure how I can go about doing this. I'd prefer to do it in XAML instead of C#, but both solutions are fine.


For reference, here's my XAML so far:

    </data:DataGridTextColumn>

        <data:DataGridTextColumn Header="Payee" 
                                 Binding="{Binding Payee}"/>
        <data:DataGridTextColumn Header="Category"
                                 Binding="{Binding Category}"/>
        <data:DataGridTextColumn Header="Memo" 
                                 Binding="{Binding Memo}"/>
        <data:DataGridTextColumn Header="Inflow" 
                                 Binding="{Binding Inflow}"/>
        <data:DataGridTextColumn Header="Outflow" 
                                 Binding="{Binding Outflow}"/>
    </data:DataGrid.Columns>
+1  A: 

You can use a IValueconverter based on string.Format and bind it in XAML.

Check out how to do it here

Yet another article to address the same

funwithcoding
+1  A: 

If you have the possibility to wait for the release of Silverlight 4, possibly at Mix10 next week. It has some new features in the binding, like formatting. With Silverlight 4 you can do stuff like:

<TextBox Text="{Binding ReleaseDate, StringFormat='MMM dd, yyyy', 
                    Mode=TwoWay}" />

Shawn Wildermuth has a nice overview of the new features on his blog.

Sorskoot