我有一些数据显示在 DataGrid 中。数据来自 SQL,其中“money”类型可以为空。当我显示此数据时,当我以双列格式将数据格式化为货币时,一切正常。IE:
内部无效填充网格(){
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);
}
但是,然后我将数据保存为 .csv 文件。当我读回该文件时,不允许将“FlatMinAmount”的值为 null 的值插入到 double 类型的列中,因此我无法将数据列格式化为 double 类型,因为许多为 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 然后在代码隐藏中设置为 r)
如果它们是字符串,那么我不能在绑定中使用 StringFormat=C 来显示 $ 等。此外,我似乎无法设置 DataGridTextColumn 的样式。那么,如何将我从 .csv 读取的值显示为 $xx.xx?