0

我有一些数据显示在 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?

4

3 回答 3

0

使用Double.Parse()- http://msdn.microsoft.com/en-us/library/system.double.parse.aspx在您读入时格式化值。这将允许您使用双精度,然后您可以应用适当的格式用于输出。

更新:从文件读取值时您似乎遇到的问题是null,从数据库中读取时它是DBNull. 默认情况下DataTable允许DBNull值。如果您想模仿此内容进行阅读,请尝试以下操作

    DataTable r = new DataTable();
    try
    {
        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)
                {
                    double d;
                    double? val2 = null;
                    double? val3 = null;
                    double? val4 = null;
                    if (Double.TryParse(row[2], out d)) val2 = d;
                    if (Double.TryParse(row[3], out d)) val3 = d;
                    if (Double.TryParse(row[4], out d)) val4 = d;

                    r.Rows.Add(row[0], row[1], val2, val3, val4, row[5], row[6]);
                }

            }
        }
于 2012-11-07T14:55:59.553 回答
0

使用TargetNullValue属性:

<dg:DataGridTextColumn Binding=""{Binding FlatMinAmount,TargetNullValue='Not Specified',StringFormat=C}" Header="FlatMinAmount" Width="Auto" IsReadOnly="True"/>
于 2012-11-07T15:03:07.867 回答
0

如果你知道这个数字是空的,你不能把列的值设置为 DBNull 吗?

row["FlatMinAmount"] = DBNull.Value;

这是我运行的一个小控制台应用程序来展示它的工作原理:

课堂节目
    {
        静态无效主要(字符串[] args){
            数据表 tbl = 新数据表();
            DataColumn col = new DataColumn("Blah", typeof(double));
            tbl.Columns.Add(col);

            数据行行 = tbl.NewRow();
            行[“废话”] = DBNull.Value;

            Console.WriteLine("结果:" + row["Blah"].ToString());
            Console.ReadKey();

        }
    }
于 2012-11-07T16:40:30.787 回答