我正在尝试更新从数据库中检索到的 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 的数据库中。
非常感谢任何帮助。
谢谢。