0

Edit: I tried the StringFormat, it's working fine but the DataGrid showing only those columns that I've included in <DataGrid.Columns> Actually only VALUE and DATE should be formatted, remaining columns should stay intact. Means now I've to manually write DataGridTextColumn for each and every column? ( I have like 20+ columns, which can be tedious work!)

New to C# here. I have a DataGrid, which gets it's values from a DataTable. I use ExcelDataReader to import from Excel to DataSet and eventually convert it to DataTable.

• How can I change the format of column DATE to system's default format? (There will be different date format in different systems)
• How can I change the format of column VALUE to exactly 2 decimal places?

In VB.NET, It was simple : DataGridView1.Columns(6).DefaultCellStyle.Format = "N2"

I cannot seem to get that work here. Please suggest me the best way to change those specific columns to the formats mentioned about. (without Performance degradation, saw some posts involving loops and converters)

Dante123
  • 45
  • 7
  • Possible duplicate of [Format values in a Datagrid](https://stackoverflow.com/questions/4299329/format-values-in-a-datagrid) – Bizhan Jun 24 '18 at 15:54
  • You are looking for `StringFormat` – Bizhan Jun 24 '18 at 15:55
  • I did try the code given in the link. But it's showing only the VALUE Column, not the rest. I have various columns, VALUE and DATE are ones among them. Can you please post a code to it? – Dante123 Jun 24 '18 at 16:09
  • `` and `` according to the link – Bizhan Jun 24 '18 at 18:12
  • Yes I know that. But what about the remaining columns? When I put those two lines, only two columns were showing. I've edited the question, please read again. – Dante123 Jun 24 '18 at 18:41

2 Answers2

2

You could handle the AutoGeneratingColumn event:

private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    switch (e.PropertyName)
    {
        case "VALUE":
            e.Column = new DataGridTextColumn
            {
                Header = e.PropertyName,
                Binding = new Binding(e.PropertyName)
                {
                    StringFormat = "N2"
                }
            };
            break;
        case "DATE":
            e.Column = new DataGridTextColumn
            {
                Header = e.PropertyName,
                Binding = new Binding(e.PropertyName)
                {
                    StringFormat = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern
                }
            };
            break;
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88
1

Just like VB. You can find all numeric string formats here (MSDN)

<DataGridTextColumn Binding="{Binding Path=VALUE, StringFormat=N2}" Header="Value" />

Current culture can be set as described here

CultureInfo.DefaultThreadCurrentCulture = newCulture;
CultureInfo.DefaultThreadCurrentUICulture = newCulture;
Thread.CurrentThread.CurrentCulture = newCulture;
Thread.CurrentThread.CurrentUICulture = newCulture;

You may also find this post useful

You may also use converters for more advanced modifications.

Bizhan
  • 16,157
  • 9
  • 63
  • 101
  • Please read the edit part in the question. The above solution is working fine. But I have various columns ( like 20+ ), means I have write `DataGridTextColumn` manually for each and every column?! – Dante123 Jun 25 '18 at 07:14
  • @Dante123 Yes that's how it works. you generally have to write one line of `DataGridXColumn` for each column regardless of how you want to format those columns. – Bizhan Jun 25 '18 at 11:47