gpt4 book ai didi

c# - DataRow 在更新 Datatable 时将小数点的小数部分归零

转载 作者:可可西里 更新时间:2023-11-01 08:29:36 24 4
gpt4 key购买 nike

我正在尝试更新从数据库中检索到的 DataTable,然后再将其绑定(bind)到 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 之后并且一旦它们被绑定(bind),输出就是 800.00 和 500.12。

theRows[0]["stockitem_pxprice"]theRows[0]["stockitem_cashprice"] 都是 decimal(5,2) 在填充 searchResultsDT 的数据库上。

非常感谢任何帮助。

谢谢。

最佳答案

是的,您在将值设置到网格中时缺少 string.format。在设置之前,您需要先格式化 double。

比如说,如果你得到一个像 4.506 这样的数字,它会显示类似 4.5060 的东西,或者如果你有一个像 4.5 这样的数字,那么它会显示为 4.50。

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

关于c# - DataRow 在更新 Datatable 时将小数点的小数部分归零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13952812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com