Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGridView中的RowsAdded事件仅对前2行触发_C#_Datagridview - Fatal编程技术网

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

C# DataGridView中的RowsAdded事件仅对前2行触发,c#,datagridview,C#,Datagridview,有没有人遇到过这样的问题:RowsAdded事件只触发两次,然后离开循环 我通过编程填充了一个DataGridView,然后执行了一些计算以给出适当的数字 下面是我的代码,但运行时,DataGridView会提取并显示所有数据,但计算字段仅对前两行执行。通过使用断点,此操作将正确循环两次,但随后将离开第三行和其他行,而不会循环通过RowsAdded事件 private void dataGridView1_RowsAdded(object sender, DataGridViewRows

有没有人遇到过这样的问题:
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;
            }

        }
    }

我设法想出了一个解决办法,但我仍然不确定为什么会这样。最后,我不得不在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;
                }
            }
        }   

    }

今天,我在一个旧应用程序中遇到了同样的问题。实际上,问题是“我们错误地使用了”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
}
for(int i=0;i