2

RowsAddedイベントが2回だけ発生し、その後ループを離れるという問題に遭遇した人はいますか?

私はプログラムでaにデータを入力DataGridViewし、適切な数値を与えるためにいくつかの計算を実行しています。

私のコードは以下のとおりですが、DataGridViewプルを実行するとすべてのデータが表示されますが、計算されたフィールドは最初の2行に対してのみ実行されます。RowsAddedブレークポイントを使用することにより、これは2回正しくループしますが、イベントをループせずに3行目以降を残します。

    private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
    {
        DataClasses1DataContext getIsEmployed = new DataClasses1DataContext();
        var result = from a in getIsEmployed.writers
                     where a.name == dataGridView1.Rows[e.RowIndex].Cells["nameDataGridViewTextBoxColumn"].Value.ToString()
                     select a;

        foreach (var b in result)
        {
            if (b.IsEmployed == true)
            {
                int val1 = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["articlesPerWeekDataGridViewTextBoxColumn"].Value);
                decimal val2 = val1 * 300;
                decimal hourlyRate = Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells["hourlyRateDataGridViewTextBoxColumn"].Value);
                int contractedEmployedHours = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["hoursDataGridViewTextBoxColumn"].Value);
                decimal employedWage = (((hourlyRate * contractedEmployedHours) * 52) / 12);

                decimal employedBonusPerArticle = Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells["bonusDataGridViewTextBoxColumn"].Value);
                decimal maximumEmployedBonus = (((val1 * employedBonusPerArticle) * 52) / 12);

                decimal maximumEmployedLiability = employedWage + maximumEmployedBonus;

                dataGridView1.Rows[e.RowIndex].Cells["ExpectedWeeklyWords"].Value = val2;
                dataGridView1.Rows[e.RowIndex].Cells["CostOfContent"].Value = employedWage;
                dataGridView1.Rows[e.RowIndex].Cells["MaximumBonus"].Value = maximumEmployedBonus;
                dataGridView1.Rows[e.RowIndex].Cells["MaximumLiability"].Value = maximumEmployedLiability;
            }
            else
            {
                int val1 = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["articlesPerWeekDataGridViewTextBoxColumn"].Value);
                decimal val2 = val1 * 300;
                int basicCost = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["articlesPerWeekDataGridViewTextBoxColumn"].Value);
                int calculatedBasicCost = (((basicCost * 3) * 52) / 12);

                decimal bonusPerArticle = Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells["bonusDataGridViewTextBoxColumn"].Value);
                decimal maximumBonus = (((val1 * bonusPerArticle) * 52) / 12);

                decimal maximumLiability = calculatedBasicCost + maximumBonus;


                dataGridView1.Rows[e.RowIndex].Cells["ExpectedWeeklyWords"].Value = val2;
                dataGridView1.Rows[e.RowIndex].Cells["CostOfContent"].Value = calculatedBasicCost;
                dataGridView1.Rows[e.RowIndex].Cells["MaximumBonus"].Value = maximumBonus;
                dataGridView1.Rows[e.RowIndex].Cells["MaximumLiability"].Value = maximumLiability;
            }

        }
    }
4

2 に答える 2

1

私はなんとか回避策を見つけることができましたが、なぜそれが起こるのかまだわかりません。最終的に、DataGridviewでRowStateChangedイベントを使用し、状態の変化を「表示」に変更するためのチェックを実行してから、計算を実行する必要がありました。

使用されるコードは以下のとおりです。

    private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
    {               
        if(e.StateChanged.ToString() == "Displayed")
        {

            DataClasses1DataContext getIsEmployed = new DataClasses1DataContext();
            var result = from a in getIsEmployed.writers
                         where a.name == dataGridView1.Rows[e.Row.Index].Cells["nameDataGridViewTextBoxColumn"].Value.ToString()
                         select a;

            foreach (var b in result)
            {
                if (b.IsEmployed == true)
                {
                    int val1 = Convert.ToInt32(dataGridView1.Rows[e.Row.Index].Cells["articlesPerWeekDataGridViewTextBoxColumn"].Value);
                    decimal val2 = val1 * 300;
                    decimal hourlyRate = Convert.ToDecimal(dataGridView1.Rows[e.Row.Index].Cells["hourlyRateDataGridViewTextBoxColumn"].Value);
                    int contractedEmployedHours = Convert.ToInt32(dataGridView1.Rows[e.Row.Index].Cells["hoursDataGridViewTextBoxColumn"].Value);
                    decimal employedWage = (((hourlyRate * contractedEmployedHours) * 52) / 12);

                    decimal employedBonusPerArticle = Convert.ToDecimal(dataGridView1.Rows[e.Row.Index].Cells["bonusDataGridViewTextBoxColumn"].Value);
                    decimal maximumEmployedBonus = (((val1 * employedBonusPerArticle) * 52) / 12);

                    decimal maximumEmployedLiability = employedWage + maximumEmployedBonus;

                    dataGridView1.Rows[e.Row.Index].Cells["ExpectedWeeklyWords"].Value = val2;
                    dataGridView1.Rows[e.Row.Index].Cells["CostOfContent"].Value = employedWage;
                    dataGridView1.Rows[e.Row.Index].Cells["MaximumBonus"].Value = maximumEmployedBonus;
                    dataGridView1.Rows[e.Row.Index].Cells["MaximumLiability"].Value = maximumEmployedLiability;
                }
                else
                {
                    int val1 = Convert.ToInt32(dataGridView1.Rows[e.Row.Index].Cells["articlesPerWeekDataGridViewTextBoxColumn"].Value);
                    decimal val2 = val1 * 300;
                    int basicCost = Convert.ToInt32(dataGridView1.Rows[e.Row.Index].Cells["articlesPerWeekDataGridViewTextBoxColumn"].Value);
                    int calculatedBasicCost = (((basicCost * 3) * 52) / 12);

                    decimal bonusPerArticle = Convert.ToDecimal(dataGridView1.Rows[e.Row.Index].Cells["bonusDataGridViewTextBoxColumn"].Value);
                    decimal maximumBonus = (((val1 * bonusPerArticle) * 52) / 12);

                    decimal maximumLiability = calculatedBasicCost + maximumBonus;


                    dataGridView1.Rows[e.Row.Index].Cells["ExpectedWeeklyWords"].Value = val2;
                    dataGridView1.Rows[e.Row.Index].Cells["CostOfContent"].Value = calculatedBasicCost;
                    dataGridView1.Rows[e.Row.Index].Cells["MaximumBonus"].Value = maximumBonus;
                    dataGridView1.Rows[e.Row.Index].Cells["MaximumLiability"].Value = maximumLiability;
                }
            }
        }   

    }
于 2013-02-05T15:35:53.037 に答える