Reputation: 2499
I am trying to format my cells inside wpf DataGrid
control but i am having problems with it.
I am having class like this:
class WashItem
{
private DateTime _Time = DateTime.Now;
private string _Staff = "Undefined";
private double _Price = 850;
public string Staff { get => _Staff; set => _Staff = value; }
public DateTime Time { get => _Time; set => _Time = value; }
public double Price { get => _Price; set => _Price = value; }
}
and I am populating my datagrid like this
private void ReloadData()
{
string[] lines = File.ReadAllLines("baza.txt");
double ukupnaVrednost = 0;
DataTable table = new DataTable();
DataColumn col1 = new DataColumn("Osoblje");
DataColumn col2 = new DataColumn("Vreme");
DataColumn col3 = new DataColumn("Cena");
table.Columns.Add(col1);
table.Columns.Add(col3);
table.Columns.Add(col2);
for (int i = 0; i < lines.Length; i++)
{
WashItem item = JsonConvert.DeserializeObject<WashItem>(lines[i]);
DataRow row = table.NewRow();
row["Osoblje"] = item.Staff;
row["Vreme"] = item.Time;
row["Cena"] = item.Price;
table.Rows.Add(row);
ukupnaVrednost += item.Price;
}
dataGridView.ItemsSource = table.AsDataView();
UkupnoOprano.Content = "Ukupno oprano vozila: " + lines.Length;
UkupnoOpranoVrednost.Content = "Vrednost: " + ukupnaVrednost.ToString("#,##0.00");
}
then I have created datagrid like this
<DataGrid Name="dataGridView" AutoGenerateColumns="true" AutoGeneratingColumn="dataGridView_AutoGeneratingColumn"></DataGrid>
and finally here is my dataGridView_AutoGeneratingColumn
function
private void dataGridView_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if(e.PropertyName == "Cena")
{
((DataGridTextColumn)e.Column).Binding.StringFormat = "#,##0.00 rsd";
}
else if(e.PropertyName == "Vreme")
{
((DataGridTextColumn)e.Column).Binding.StringFormat = "dd/MM/yyyy";
}
}
Auto generate columns function get's fired and it does enter if
block but at the end my data is still the same - not formatted.
Upvotes: 0
Views: 48
Reputation: 35680
I would say you need to set DataType for DataColumns, because required formats are specific to those types, and won't work for object
type.
DataTable table = new DataTable
{
Columns =
{
new DataColumn("Osoblje"),
new DataColumn("Vreme", typeof(DateTime)),
new DataColumn("Cena", typeof(double))
}
};
Upvotes: 1