【问题标题】:Converting double to float C# WPF将双精度转换为浮点 C# WPF
【发布时间】:2015-05-06 11:08:35
【问题描述】:

我需要在我的 tblSysproStock 表中的数据网格中显示信息。该表中有包含浮点值的列,在我的应用程序中,我需要将 double 值转换为 float

我想要填充数据网格的编码:

private void FillSysproDataGrid()
{
    using (DataClassesDataContext DC = new DataClassesDataContext())
    {
        dgSysproStock.ItemsSource = DC.tblSysproStocks.Where<tblSysproStock>(c => c.StockID != null)
            .Select<tblSysproStock, SSData>(m => new SSData()
            {
                SID = m.StockID,
                SCode = m.StockCode,
                SDescription = m.StockDescription,
                SConvFactAltUom = (float)m.ConvFactAltUom,
                SConvMulDiv = m.ConvMulDiv,
                SConvFactOthUom = (float)m.ConvFactOthUom,
                SMulDiv = m.MulDiv,
                SMass = (float)m.Mass,
                SUpdatedSupplier = m.UpdatedSupplier,
                SCycleCount = (float)m.CycleCount,
                SProductClass = (float)m.ProductClass,
                SUnitCost = (float)m.UnitCost,
                SSegal = m.Segal,
                SWareHouse = m.Warehouse,
                SMinimumStock = (float)m.MinimumStock,
                SMaximumStock = (float)m.MaximumStock,
                SStockForNow = (float)m.StockForNow,
                SStockCount = m.StockCount,
                SValue = (float)m.Value,
            });
    }
}

我将表中的值与我的 SSDData 类相等:

public struct SSData
{
    public string _ss;

    public int SID { get; set; }
    public string SCode { get; set; }
    public string SDescription { get; set; }
    public float SConvFactAltUom { get; set; }
    public string SConvMulDiv { get; set; }
    public float SConvFactOthUom { get; set; }
    public string SMulDiv { get; set; }
    public float SMass { get; set; }
    public string SUpdatedSupplier { get; set; }
    public float SCycleCount { get; set; }
    public float SProductClass { get; set; }
    public float SUnitCost { get; set; }
    public string SSegal { get; set; }
    public string SWareHouse { get; set; }
    public float SMinimumStock { get; set; }
    public float SMaximumStock { get; set; }
    public float SStockForNow { get; set; }
    public string SStockCount { get; set; }
    public float SValue { get; set; }
}

现在我得到的错误是 -

null 值不能分配给 System.Single 类型的成员,这是一个不可为 null 的值类型。当我运行我的应用程序时。

我认为问题出在转换上?任何有任何建议或解决方案的人都会很棒!谢谢你。

编辑:@Greg,好的,所以我使用了新的编码,这是一个 sn-p:

    using (DataClassesDataContext DC = new DataClassesDataContext())
    {
        dgSysproStock.ItemsSource = DC.tblSysproStocks.Where<tblSysproStock>(c => c.StockID != null)
            .Select<tblSysproStock, SSData>(m => new SSData()
            {
                SID = m.StockID,
                SCode = m.StockCode,
                SDescription = m.StockDescription,
                float.TryParse(m.ConvFactAltUom, out SConvFactAltUom),
            });
    }

大声笑,我得到的错误是 -

初始化器成员声明器无效

我对编码非常陌生,我认为我可能使用了您提供的编码方式错误?

【问题讨论】:

    标签: c# wpf linq datagrid


    【解决方案1】:

    你可以使用

     float.TryParse(m.ConvFactAltUom, out SConvFactAltUom);
    

    只是为了确保它有效。如果可能,它会将值转换为浮点数,或者保持属性不变并且不会引发异常。

    【讨论】:

    • 嗨!感谢您的答复。您给出的编码给了我这个错误:“as 运算符必须与引用类型或可空类型一起使用('float' 是不可为空的值类型)”。
    • 编辑时缺少“?”
    • 添加的“?”仍然给我错误:(. 'Cannot convert type 'double?' to 'float?' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion or null type conversion'.
    • @CareTaker22 抱歉,您可以使用我的错误??仅使用对象,请参阅编辑后的答案。
    • @Greg 我使用了编码,但是请您看看我在原始问题中所做的编辑,谢谢。
    猜你喜欢
    • 2015-06-12
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多