求和整数并使用C#向TextBox显示结果

时间:2016-11-28 09:38:09

标签: c#

在求和整数时我遇到了一些问题,并使用C#将结果显示给TextBox。当我运行我的代码时,结果只显示lim30den50tot的结果。我想总结lim30den50totlim60den50tot等等。我该怎么做?

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();
        }
    }
}

2 个答案:

答案 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();
        }
    }
}

答案 1 :(得分: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();