12

Gridview にバインドする前に、DB から取得した DataTable を更新しようとしています。

ただし、小数フィールドを更新すると、小数点以下の部分がゼロになります。私は何が欠けていますか?

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 が取り込まれた DB にあります。

どんな助けでも大歓迎です。

ありがとう。

4

4 に答える 4

2

はいstring.format、グリッドに値を設定しているときに欠落しています。設定する前に double をフォーマットする必要があります。

たとえば、4.506 のような数値を取得した場合は 4.5060 のように表示され、4.5 のような数値を取得した場合は 4.50 と表示されます。

テンプレート化されたグリッドビューでこの問題に直面し、string.format とフォーマット指定子を使用して解決する必要がありました。

于 2013-10-28T12:49:08.363 に答える
0

あなたのテーブルには、intデータ型として以下の2つのフィールドがあると思います。

  • stockitem_pxprice

  • stockitem_cashprice

numeric (18,2)これらの2つのフィールドと同様にデータ型でテーブルを変更します

また

[decimal](18, 2) これらの2つのフィールドと同様にデータ型でテーブルを変更します

型キャストの必要はありません。asp.netは暗黙的にそれを行います

于 2013-01-19T10:15:43.040 に答える
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 のデータ フィールドを結果セットから Decimal 値として取得することを意味します。これは、SELECT コマンドで選択した列の順序です。また、ストアド プロシージャgetAllMyPriceMatchesでは、2 つのテーブル結果の間でJOINコマンドを使用してクエリ スクリプトを変更できます。その場合、2 番目のクエリ スコープは必要ありません。

于 2013-11-12T03:10:15.453 に答える
0

を foreach し、theRows.Columnsトレースする .Type プロパティをダンプします。型はTSQL 型であるdecimal(5,2)であると何度か言及しました。DataTable は、10 進数であり、非常に大きな float のような c# 型を保持します。ここで重要なのは c# の型です。

Select myVal * 1たとえば、myVal の 10 進数を int に変換するなど、tsql で変換が行われた可能性があります。それがあなたのデータテーブルが保持する型になります。私は通常、myVal *1.0値が int に変換されないように定数を設定します。C# で 100m のように 10 進数を宣言する必要があるのと同じように、TSQL では、データ型の変換を防ぐためにリテラルが特別に宣言されていることを確認する必要があります。

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

于 2013-04-21T01:22:49.833 に答える