Q: Format autogenerated column in WPF DataGrid I have some data retrieved from SQLite through an SQLite Adapter that I fill into a DataTable, like this:
using (conexionSQLite = new SQLiteConnection(Datos.stringConexionSQLite))
{
    conexionSQLite.Open();
    string textoSQLite;
    SQLiteDataAdapter adaptadorSQLite;
    try
    {
        textoSQLite = "SELECT Fecha, ROUND(ValorTotal,0) as ValorTotal, ROUND(ValorProductoTerminado,0) as ValorProductoTerminado, ROUND(ValorSemielaborados,0) as ValorSemielaborados, ROUND(ValorMateriasPrimas,0) as ValorMateriasPrimas, ROUND(ValorEnvases,0) as ValorEnvases FROM valorStockPorFecha";
        adaptadorSQLite = new SQLiteDataAdapter(textoSQLite, conexionSQLite);
        adaptadorSQLite.Fill(bd, "tablaValorStock");
    }
    catch (SQLiteException excepcionSQL)
    {
        Console.WriteLine("Error: " + excepcionSQL.ToString());
    }
}
DataTable tablaValorStock = bd.Tables["tablaValorStock"];

Then I bind it to a DataGrid:
dataGrid.ItemsSource = tablaValorStock.DefaultView;

That generates all the columns automatically.
What I want is to format some of the columns, without having to define them manually first.
I was thinking some kind of event handling of autogenerating columns, something similar to this code which is for Windows Forms' DataGrid equivalent:
private void RadGridView1_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
{
    GridViewDataColumn column = e.Column as GridViewDataColumn;
    if (column.DataType == typeof(int))
    {
        column.DataFormatString = "{0:p}";                             
    }
}

I would assing one format or another based on DataGridColumn.Header

A: I know this is an old post, but I met this issue recently. I post a solution here in case it helps anyone. Here is what I did in AutoGeneratingColumn:

if (e.PropertyType == typeof(int))
{
    DataGridTextColumn dataGridTextColumn = e.Column as DataGridTextColumn;
    if (dataGridTextColumn != null)
    {
        dataGridTextColumn.Binding.StringFormat = "{0:p}";
    }
 }

As @Hardgraf suggests, it would be much better to specify it in XAML for most of the cases. However, here is why I need this to be done in C# code. I have to display around 20 tables returned from a stored procedure. The number of the tables and the column names are totally decided by the stored procedure. It would be pretty hard to do it in XAML if possible. Even I managed to make it work in XAML, the code would be pretty hard to read. But when I do this in C# code, a simple loop and an event handler to the AutoGeneratingColumn will do the trick elegantly.
