I am trying to update a DataTable that is retrieved from a DB before binding it to a Gridview.
However, when I update decimal fields the part after the decimal point is zeroised. What am I missing?
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();
I have output PX and Cash before the assignment and they hold the correct values of 800.19 and 500.12, but after the AcceptChanges and once they are bound, the output is 800.00 and 500.12.
theRows[0]["stockitem_pxprice"]
& theRows[0]["stockitem_cashprice"]
are both decimal(5,2)
on the DB where the searchResultsDT is populated from.
Any help greatly appreciated.
Thanks.
Yes you are missing string.format
while setting value into grid. You need to format double before setting it.
Say for if you get a number like 4.506 it will display something like 4.5060 or if you have a number like 4.5 then it will display as 4.50.
I have faced this problem in templated gridview and had to use string.format and format specifier to resolve it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With