2016-07-16 16 views
1

商品の合計金額を表示したいと思います。私はあなたが明確な説明のためにimageで問題を確認することができます合計金額 合計を表示するデータテーブルの列の値の合計

.00を追加したい

それは私の項目の間違った合計を見せて
  • の価格:私は2つの問題に直面しています。あなたは常に合計のSelectedItemの行に追加するにはループでは

    tDisplay.Text = "Return/" + "Receipt No:" + Return_Form.setalueforText011; 
    label1.Text = Return_Form.setalueforText011; 
    
    OleDbConnection VCON = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Restaurant.accdb"); 
    DataSet dsa = new DataSet(); 
    DataTable dt = new DataTable(); 
    dsa.Tables.Add(dt); 
    
    OleDbDataAdapter da = new OleDbDataAdapter(); 
    da = new OleDbDataAdapter("SELECT [Column1],[Column2],[Column3] from [Total] Where [Receipt No] = " + label1.Text + "", VCON); 
    da.Fill(dt); 
    //dataGridView1.DataSource = dt; 
    
    for (int i = 0; i < dt.Rows.Count; i++) 
    { 
        products.Add(new tblProduct() { productName = dt.Rows[i]["Column2"].ToString(),productPrice = Convert.ToDecimal(Math.Round(Convert.ToDecimal(dt.Rows[i]["Column1"].ToString())))}); 
        label3.Text = dt.Rows[i]["Column3"].ToString(); 
        textBox59.Text = "Rs: "+String.Format("{0:}", Total); 
        tblProduct selected = (tblProduct)(listBox60.SelectedItem); 
        Total += (decimal)selected.productPrice; 
    } 
    VCON.Close(); 
    

答えて

1

は、ここに私のコードです。これは常に最初の項目なので、最初の項目の値が倍になります。

for (int i = 0; i < dt.Rows.Count; i++) 
{ 
    // Create and initialize a new tblProduct from the datatable row 
    tblProduct current = new tblProduct(); 
    current.ProductName = dt.Rows[i]["Column2"].ToString(); 
    current.productPrice = Convert.ToDecimal(Math.Round(Convert.ToDecimal(dt.Rows[i]["Column1"].ToString()))); 

    // Add to your list of products 
    products.Add(current); 

    // This line is wrong because you overwrite the value at each loop 
    label3.Text = dt.Rows[i]["Column3"].ToString(); 

    // Sum the price of the current tblProduct 
    Total += (decimal)current.productPrice; 
} 
// Outside the loop update your total label 
textBox59.Text = "Rs: "+String.Format("{0:0.00}", Total); 

私に助言を与えることができれば。あなたのコントロールをそのように名付けてはいけません。彼らは読むことができず、容易に認識できません。今からこのコードを見ると、どのコントロールがtextBox59かlistBox60かを覚えておくのに多くの問題があります。

+0

ありがとうございます私は本当にありがとう、あなたは本当に天才な人を殺しているリストボックス –

+0

と私の最初の質問であなたに尋ねるもう一つのことについては気にしない場合はもう一度質問をすることができますか? –

+0

あなたの質問のこの部分は私には不明です。 ListBoxに項目を追加する方法がわかりません。しかし、商品を商品リストから取り出した場合は、今のようにフォーマットする必要があります。 – Steve

1

forループを使用する代わりに、単にComputeデータテーブルのメソッドを使用して、式を渡して計算することができます。たとえば:

  • The "0" Custom Numeric Format String

    var total = double.Parse(yourDataTable.Compute("SUM(Column1)", "").ToString()); 
    

    はまた、小数点以下2桁を表示するために、合計の書式を設定するには、これらのオプションのいずれかを使用することができますstring.Format("{0:0.00}", total))

  • The Fixed-Point "F" format Specifierstring.Format("{0:F2}", total))

関連する問題