12

我正在尝试更新从数据库中检索到的 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();

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

theRows[0]["stockitem_pxprice"]&theRows[0]["stockitem_cashprice"]decimal(5,2)在从中填充 searchResultsDT 的数据库中。

非常感谢任何帮助。

谢谢。

4

4 回答 4

2

是的,您string.format在将值设置为网格时丢失了。您需要在设置之前格式化双精度。

假设如果你得到一个像 4.506 这样的数字,它将显示像 4.5060 或者如果你有像 4.5 这样的数字,那么它将显示为 4.50。

我在模板化的 gridview 中遇到了这个问题,不得不使用 string.format 和格式说明符来解决它。

于 2013-10-28T12:49:08.363 回答
0

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

  • stockitem_pxprice

  • stockitem_cashprice

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

或者

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

不需要类型转换,asp.net 会隐式执行

于 2013-01-19T10:15:43.040 回答
0

ForeachtheRows.Columns并转储 .Type 属性以进行跟踪。您已经多次提到类型是十进制(5,2),它是一种 TSQL 类型。DataTable 包含 c# 类型,它是十进制的,就像一个非常大的浮点数。这里重要的是 c# 类型。

您的 tsql 中可能进行了转换,例如Select myVal * 1将您的 myVal 十进制转换为 int。那将是您的数据表保存的类型。我通常将我的常量设置myVal *1.0为防止值变成整数。就像你必须在 c# 中声明一个小数一样,比如 100m,在 TSQL 中你必须确保专门声明文字以防止数据类型转换

参考http://msdn.microsoft.com/en-us/library/ms179899.aspx

于 2013-04-21T01:22:49.833 回答
0

我不确定你是否有答案,但我会分享我可以通过使用SqlDataReader的方法来解决这个问题,它是GetDecimal方法。你可以这样编码

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();

    decimal px = 0;
    decimal cash = 0;

    if (pcReader.Read())
    {
        px = pcReader.GetDecimal(0);
        cash = pcReader.GetDecimal(1);
    }
    pcReader.Close();
    stockConn.Close();
}

其中pcReader.GetDecimal(0)表示从结果集中获取索引 0 处的数据字段作为十进制值,这是您在 SELECT 命令中选择的列的顺序。和存储过程getAllMyPriceMatches,您可以使用JOIN命令在两个表结果之间修改查询脚本,那么您不需要第二个查询范围。

于 2013-11-12T03:10:15.453 回答