2016-11-28 7 views
0

整数を合計して結果をTextBoxにC#で表示するときに問題が発生しました。コードを実行すると、その結果はlim30den50totという結果になります。私はlim30den50totlim60den50totなどを合計したいと思います。どうすればいいですか?C#を使用して整数を合計してテキストボックスに結果を表示

private int lim30den50tot; 
private int lim60den50tot; 
private int lim150den50tot; 
private int lim450den50tot; 
private int lim600den50tot; 


private void JumlahLembar() 
{ 
    int lim30 = 0; 
    int lim60 = 0; 
    int lim150 = 0; 
    int lim450 = 0; 
    int lim600 = 0; 

    foreach(DataGridViewRow row in JadwalisiGV.Rows) 
    { 

     if (!row.IsNewRow) 
     { 


      OleDbConnection kon = new OleDbConnection(koneksi); 
      OleDbCommand command = kon.CreateCommand(); 

      kon.Open(); 
      command.CommandText = "select * from [StokLembar$] where [Limit] = " + row.Cells["Limit"].Value; 
      OleDbDataReader reader = command.ExecuteReader(); 

      while (reader.Read()) 
      { 
       int den50 = int.Parse(reader["Lembar Denom 50"].ToString()); 
       int den100 = int.Parse(reader["Lembar Denom 100"].ToString()); 

       if (row.Cells["Limit"].Value.ToString() == "30") 
       { 
        lim30++; 
        lim30den50tot = lim30 * 2 * den50 * 50000; 
       } 

       else if (row.Cells["Limit"].Value.ToString() == "60") 
       { 
        lim60++; 
        lim60den50tot = lim60 * 2 * den50 * 50000; 
       } 

       else if (row.Cells["Limit"].Value.ToString() == "150") 
       { 
        lim150++; 
        lim150den50tot = lim150 * 2 * den50 * 50000; 
       } 

       else if (row.Cells["Limit"].Value.ToString() == "450") 
       { 
        lim450++; 
        lim450den50tot = lim450 * 2 * den50 * 50000; 
       } 

       else if (row.Cells["Limit"].Value.ToString() == "600") 
       { 
        lim600++; 
        lim600den50tot = lim600 * 2 * den50 * 50000; 
       } 


       TotalDen50Box.Text = (lim30den50tot + lim600den50tot + lim150den50tot + lim450den50tot + lim600den50tot).ToString(); 
       } 
      kon.Close(); 
     } 
    } 
} 

答えて

0

を使用すると、ループの各反復でそれを上書きしているので、あなたのループの外TotalDen50Box.Textを入れてください。また、すべての行の合計を表示する場合は、整数値は現在の値と前の値の和になる必要があります。例lim30den50tot += lim30 * 2 * den50 * 50000;

while(reader.Read()) 
{ 
    //stuff 

    if(condition) 
    { 
     lim30den50tot += lim30 * 2 * den50 * 50000; 
    } 
    else if(condition2) 
     //do the same for other ints 
} 

TotalDen50Box.Text = (lim30den50tot + lim600den50tot + lim150den50tot + lim450den50tot + lim600den50tot).ToString(); 
+0

あなたの答えをありがとう。できます :) –

0

問題は、各ループで結果を表示することです。あなただけのループの外TotalDen50Box.Text = (lim30den50tot + lim600den50tot + lim150den50tot + lim450den50tot + lim600den50tot).ToString();を入れています

private int lim30den50tot; 
private int lim60den50tot; 
private int lim150den50tot; 
private int lim450den50tot; 
private int lim600den50tot; 


private void JumlahLembar() 
{ 
    int lim30 = 0; 
    int lim60 = 0; 
    int lim150 = 0; 
    int lim450 = 0; 
    int lim600 = 0; 

    foreach(DataGridViewRow row in JadwalisiGV.Rows) 
    { 

     if (!row.IsNewRow) 
     { 


      OleDbConnection kon = new OleDbConnection(koneksi); 
      OleDbCommand command = kon.CreateCommand(); 

      kon.Open(); 
      command.CommandText = "select * from [StokLembar$] where [Limit] = " + row.Cells["Limit"].Value; 
      OleDbDataReader reader = command.ExecuteReader(); 

      while (reader.Read()) 
      { 
       int den50 = int.Parse(reader["Lembar Denom 50"].ToString()); 
       int den100 = int.Parse(reader["Lembar Denom 100"].ToString()); 

       if (row.Cells["Limit"].Value.ToString() == "30") 
       { 
        lim30++; 
        lim30den50tot += lim30 * 2 * den50 * 50000; 
       } 

       else if (row.Cells["Limit"].Value.ToString() == "60") 
       { 
        lim60++; 
        lim60den50tot += lim60 * 2 * den50 * 50000; 
       } 

       else if (row.Cells["Limit"].Value.ToString() == "150") 
       { 
        lim150++; 
        lim150den50tot += lim150 * 2 * den50 * 50000; 
       } 

       else if (row.Cells["Limit"].Value.ToString() == "450") 
       { 
        lim450++; 
        lim450den50tot += lim450 * 2 * den50 * 50000; 
       } 

       else if (row.Cells["Limit"].Value.ToString() == "600") 
       { 
        lim600++; 
        lim600den50tot += lim600 * 2 * den50 * 50000; 
       } 



      } 
      TotalDen50Box.Text = (lim30den50tot + lim600den50tot + lim150den50tot + lim450den50tot + lim600den50tot).ToString(); 
      kon.Close(); 
     } 
    } 
} 
+0

ご回答ありがとうございます。それは動作します:) –

関連する問題