Q:

How to set the StringFormat for specific columns in a WPF Datagrid whose values are populated via ItemsSource method

I have a WPF Datagrid whose values are populated via ItemsSource method.
How would I go about doing something similar to the code below so that it's formatted decently? Also would there be a way to simply carry over the StringFormat's from the Source table?
<DataGridTextColumn Binding="{Binding Path=Date, StringFormat=d}" Header="Date" />
<DataGridTextColumn Binding="{Binding Path=Amount, StringFormat=C}" Header="Amount"/>

The table is generated through this code:
<DataGrid x:Name="datatable" ItemsSource="{Binding SubVwr.Tables[0].Tbl}"/>

A:

What you have to do is to use AutoGeneratingColumn
private void datagrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    DataGridTextColumn col = e.Column as DataGridTextColumn;
    if (col != null && e.PropertyType == typeof(double))
    {
        if (!col.Header.ToString() == "Amount")
            col.Binding = new Binding(e.PropertyName) { StringFormat = "N0" };
        else
            col.Binding = new Binding(e.PropertyName) { StringFormat = "N2" };
    }
}

Also you can define the format you want for each col by checking the col.Header.ToString()

