更新Datatable时,DataRow将小数的小数部分归零

Ben*_*ury 12 c# asp.net datatable datarow

我正在尝试更新从数据库中检索的DataTable,然后再将其绑定到Gridview.

但是,当我更新小数字段时,小数点后的部分归零.我错过了什么?

if (HttpContext.Current.Request.IsAuthenticated)
{
    // Get additional price matches
    using (SqlConnection stockConn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString))
    {
        // Check for trade match offer
        SqlCommand tradePCCheck = new SqlCommand("getAllMyPriceMatches", stockConn);
        tradePCCheck.CommandType = CommandType.StoredProcedure;
        SqlParameter email = tradePCCheck.Parameters.Add("@email", SqlDbType.NVarChar);
        try
        {
            email.Value = this.Context.User.Identity.Name;
        }
        catch
        {
            email.Value = " ";
        }
        SqlParameter thedate = tradePCCheck.Parameters.Add("@theDate", SqlDbType.DateTime);
        thedate.Value = DateTime.Now.AddHours(-50);

        stockConn.Open();
        SqlDataReader pcReader = tradePCCheck.ExecuteReader();
        pms.Load(pcReader);
        pcReader.Close();
        stockConn.Close();
    }
}

//Set Connection, Open the DB & Fill Data Set

using (SqlConnection stockConn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString))
{
    SqlCommand stockCommand = new SqlCommand("getTISearchResults", stockConn);
    stockCommand.CommandType = CommandType.StoredProcedure;
    SqlParameter keyword = stockCommand.Parameters.Add("@keyword", SqlDbType.NVarChar);
    keyword.Value = prefixText;
    stockConn.Open();
    SqlDataReader rd = stockCommand.ExecuteReader();
    searchResults.Load(rd);
    stockCommand.Dispose();
    rd.Dispose();
}

// Update Results with elevated prices...
foreach (DataRow dr in searchResults.Rows)
{
    // Check for PMS
    DataRow[] thePMS = pms.Select("tpc_stockid = '" + dr["stockitem_number"].ToString() + "'");

    if (thePMS.Length > 0)
    {
        decimal px = 0;
        decimal cash = 0;

        if (thePMS[0]["tpc_pricepx"] != null && !thePMS[0]["tpc_pricepx"].ToString().Equals(""))
        {
            px = Convert.ToDecimal(thePMS[0]["tpc_pricepx"]);
        }

        if (thePMS[0]["tpc_price"] != null && !thePMS[0]["tpc_price"].ToString().Equals(""))
        {
            cash = Convert.ToDecimal(thePMS[0]["tpc_price"]);
        }
        // update table and accept changes
        DataRow[] theRows = searchResults.Select("stockitem_number = '" + dr["stockitem_number"].ToString() + "' ");

        if (theRows.Length > 0)
        {
            theRows[0]["stockitem_pxprice"] = px;
            theRows[0]["stockitem_cashprice"] = cash;
            searchResults.AcceptChanges();
        }
    }
}

gvSearchResults.DataSource = searchResults;
gvSearchResults.DataBind();
Run Code Online (Sandbox Code Playgroud)

我在分配之前输出了PX和Cash,并且它们保持正确的值800.19和500.12,但在AcceptChanges之后,一旦它们被绑定,输出为800.00和500.12.

theRows[0]["stockitem_pxprice"]&theRows[0]["stockitem_cashprice"]decimal(5,2)位于填充searchResultsDT的DB上.

任何帮助非常感谢.

谢谢.

小智 0

我认为你的表有以下两个字段作为 int 数据类型:

  • 库存商品_px价格

  • 库存商品_现金价格

numeric (18,2)使用这两个字段的数据类型修改表

或者

[decimal](18, 2) 使用这两个字段的数据类型修改表

无需类型转换,asp.net 会隐式执行此操作