lundi 2 mars 2015

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


Aucun commentaire:

Enregistrer un commentaire