DataGridView中的RowsAdded事件仅针对前2行触发

时间:2013-02-04 21:02:28

标签: c# datagridview

是否有人遇到RowsAdded事件仅触发两次然后离开循环的问题?

我以编程方式填充DataGridView然后执行一些计算以给出适当的数字。

我的代码在下面但是在运行时DataGridView拉动并显示所有数据,但计算的字段仅针对前两行完成。通过使用断点,它可以正确地循环两次,但随后会离开第三行而其他行则不会循环遍历RowsAdded事件。

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

        }
    }

2 个答案:

答案 0 :(得分: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;
                }
            }
        }   

    }

答案 1 :(得分:0)

今天,我在一个旧的应用程序中遇到了同样的问题。实际上问题是"我们正在使用" RowsAdded "事件不正确。如果您阅读 DataGridViewRowsAddedEventArgs 类的文档,您将看到它可以包含有关添加多行的信息。

public class DataGridViewRowsAddedEventArgs : EventArgs
{
  //The number of rows that have been added.
  public int RowCount { get; }

  //The index of the first added row.
  public int RowIndex { get; }
}

因此,您必须处理从 e.RowIndex 开始的 e.RowCount 行数,以便处理所有新添加的行。

for (int i = 0; i < e.RowCount; i++)
{
   DataGridViewRow _row = m_dgMetadata.Rows[e.RowIndex + i];
   //.. Do whatever you want
}