I’m working on putting a CSV file into a DataTable. The order of the columns in the CSV can change so I have a utility that maps columns to different object values as well as their Type
There is an “Amount” column that is a Decimal. Negative numbers are masked with parentheses (e.g. “(#.##)” instead of “-#.##”). Is there a way to add the NumberStyles.AllowParentheses to the DataRow.ItemArray Decimal parser?
I like the efficiency of DataRow.ItemArray = string[] and would prefer to not go through and manually parse the decimal values.
public class BatchTable : DataTable
{
...
// INSIDE CONSTRUCTOR...
// This is the header row
foreach (string colName in csvParser.ReadFields())
{
// If the field has been mapped, then we know the datatype for the table
if (CCImportMap.ImportMappings.TryGetValue(colName, out ImportMapping mapping))
{
this.Columns.Add(colName, mapping.ColumnDataType);
}
else
{
this.Columns.Add(colName);
}
}
// Now add the rest of the batch
while (!csvParser.EndOfData)
{
// Read current line fields, pointer moves to the next line.
string[] fields = csvParser.ReadFields();
DataRow dr = this.NewRow();
dr.ItemArray = fields; // ERR: Can't parse "(#.##)" as a negative decimal value
this.Rows.Add(dr);
}
}