I have some data being shown in a DataGrid. The data comes from SQL, where a “money” type can be null. When i display this data, all is OK when i format the data in a double column format as currency. ie:
internal void FillGrid()
{
bD = new DataTable();
DataTable dt = EmployerQuery(); //queries the SQL DB
bD.Columns.Add(new DataColumn("ID", typeof(int)));
bD.Columns.Add(new DataColumn("EmployerName", typeof(string)));
bD.Columns.Add(new DataColumn("FlatMinAmount", typeof(double)));
bD.Columns.Add(new DataColumn("DistrictRate", typeof(double)));
bD.Columns.Add(new DataColumn("VendorRate", typeof(double)));
bD.Columns.Add(new DataColumn("Description", typeof(string)));
bD.Columns.Add(new DataColumn("EmployeeCount", typeof(int)));
foreach (DataRow sqlRow in dt.Rows)
{
var row = bD.NewRow();
row["ID"] = sqlRow["ClientID"];
row["EmployerName"] = sqlRow["OfficialName"];
row["FlatMinAmount"] = sqlRow["FlatMinAmount"];
row["DistrictRate"] = sqlRow["DistrictRate"];
row["VendorRate"] = sqlRow["VendorRate"];
row["Description"] = sqlRow["Description"];
row["EmployeeCount"] = sqlRow["EmployeeCount"];
bD.Rows.Add(row);
}
However, I then save the data as a .csv file. When i read that file back, the values for “FlatMinAmount” that were null are not allowed to be inserted into a column type double, so I cannot format the data columns as type double, since many are null.
<dg:DataGrid Name="newGrid" ItemsSource="{Binding ElementName=readGrid, Path=readGrid}" AutoGenerateColumns="False"
Margin="5" Height="175" Width="Auto" ColumnWidth="Auto">
<dg:DataGrid.Columns>
<dg:DataGridTextColumn Binding="{Binding ID}" Header="Employer ID" Width="Auto" IsReadOnly="True"/>
<dg:DataGridTextColumn Binding="{Binding EmployerName}" Header="Employer Name" Width="Auto" IsReadOnly="True"/>
<dg:DataGridTextColumn Binding="{Binding FlatMinAmount, StringFormat=C}" Header="FlatMinAmount" Width="Auto" IsReadOnly="True"/>
<dg:DataGridTextColumn Binding="{Binding DistrictRate, StringFormat=C}" Header="District Rate" Width="Auto" IsReadOnly="True"/>
<dg:DataGridTextColumn Binding="{Binding VendorRate, StringFormat=C}" Header="Vendor Rate" Width="Auto" IsReadOnly="True"/>
<dg:DataGridTextColumn Binding="{Binding Description}" Header="Description" Width="Auto" IsReadOnly="True"/>
<dg:DataGridTextColumn Binding="{Binding EmployeeCount}" Header="EmployeeCount" Width="*" IsReadOnly="True"/>
</dg:DataGrid.Columns>
</dg:DataGrid>
DataTable r = new DataTable();
try
{
//string csv = string.Empty;
//r.Columns.Add("ID", typeof(int));
//r.Columns.Add("EmployerName", typeof(string));
//r.Columns.Add("FlatMinAmount",typeof(double));
//r.Columns.Add("DistrictRate", typeof(double));
//r.Columns.Add("VendorRate", typeof(double));
//r.Columns.Add("Description", typeof(string));
//r.Columns.Add("EmployeeCount", typeof(int));
r.Columns.Add("ID");
r.Columns.Add("EmployerName");
r.Columns.Add("FlatMinAmount");
r.Columns.Add("DistrictRate");
r.Columns.Add("VendorRate");
r.Columns.Add("Description");
r.Columns.Add("EmployeeCount");
// Read sample data from CSV file
using (CsvFileReader reader = new CsvFileReader(filename))
{
CsvRow row = new CsvRow();
while (reader.ReadRow(row))
{
//foreach (string s in row)
{
r.Rows.Add(row[0], row[1], row[2], row[3], row[4], row[5], row[6]);
}
}
}
(readGrid is then set to r in code-behind)
If they are string, then i cannot use the StringFormat=C in the binding to show $ etc. Also, it seems I cannot style a DataGridTextColumn. So, how can i display the values I read in from .csv as $xx.xx?
If you know the number is null, can’t you just set the value of the column to DBNull?
Here’s a little console app I ran to show this working:
class Program { static void Main(string[] args) { DataTable tbl = new DataTable(); DataColumn col = new DataColumn("Blah", typeof(double)); tbl.Columns.Add(col); DataRow row = tbl.NewRow(); row["Blah"] = DBNull.Value; Console.WriteLine("Result: " + row["Blah"].ToString()); Console.ReadKey(); } }