I have a DataGridView
with AutoGenerateColumns=true.
I populate the data from CSV (so they are all string, not decimal)
DataTable dt = CsvToDataTable(masterlistCsvPath); // My function, returns a DataTable
//dt.Columns['Price'].DataType = Type.GetType("System.Decimal"); // Exception: Cannot change DataType of a column once it has data.
dgvMasterList.DataSource = dt;
How do you format the Price column as currency?
eg. "38.5" → "$38.50" or "38.50"
This doesn't work (it does nothing):
dgvMasterList.Columns["Price"].ValueType = Type.GetType("System.Decimal");
dgvMasterList.Columns["Price"].DefaultCellStyle.Format = "C";
This works (just to prove I'm calling it correctly)
dgvMasterList.Columns["Price"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
Another related question:
How do you add a percent symbol to a column?
eg. "5" → "5%"