在DataGridView中RowsAdded事件只发射了前两排 [英] RowsAdded Event in DataGridView only firing for first 2 rows

查看:155
本文介绍了在DataGridView中RowsAdded事件只发射了前两排的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已遍布了 RowsAdded 事件仅触发两次,然后退出循环问题的人来了?



我编程很填充 DataGridView的,然后进行一对夫妇计算,给予相应的数字。



我的代码以下,但在运行时的 DataGridView的拉并显示所有数据,但计算字段只完成为第2行。通过使用断点这循环通过正确两次,但随后离开第三行和其他人不通过 RowsAdded 事件循环。

 私人无效dataGridView1_RowsAdded(对象发件人,DataGridViewRowsAddedEventArgs E)
{
DataClasses1DataContext getIsEmployed =新DataClasses1DataContext();
VAR的结果=从在getIsEmployed.writers
,其中a.name == dataGridView1.Rows [e.RowIndex] .Cells [nameDataGridViewTextBoxColumn]。Value.ToString()
选择;

的foreach(在结果VAR B)
{
如果(b.IsEmployed ==真)
{
INT VAL1 = Convert.ToInt32(dataGridView1 .Rows [e.RowIndex] .Cells [articlesPerWeekDataGridViewTextBoxColumn]值)。
十进制将val2 = VAL1 * 300;
十进制hourlyRate = Convert.ToDecimal(dataGridView1.Rows [e.RowIndex] .Cells [hourlyRateDataGridViewTextBoxColumn]值。);
INT contractedEmployedHours = Convert.ToInt32(dataGridView1.Rows [e.RowIndex] .Cells [hoursDataGridViewTextBoxColumn]值。);
十进制employedWage =(((hourlyRate * contractedEmployedHours)* 52)/ 12);

十进制employedBonusPerArticle = Convert.ToDecimal(dataGridView1.Rows [e.RowIndex] .Cells [bonusDataGridViewTextBoxColumn]值。);
十进制maximumEmployedBonus =(((val1中* employedBonusPerArticle)* 52)/ 12);

十进制maximumEmployedLiability = employedWage + maximumEmployedBonus;

dataGridView1.Rows [e.RowIndex] .Cells [ExpectedWeeklyWords]值= val2的。
dataGridView1.Rows [e.RowIndex] .Cells [CostOfContent]值= employedWage;
dataGridView1.Rows [e.RowIndex] .Cells [MaximumBonus]值= maximumEmployedBonus。
dataGridView1.Rows [e.RowIndex] .Cells [MaximumLiability]值= maximumEmployedLiability。
}
,否则
{
INT VAL1 = Convert.ToInt32(dataGridView1.Rows [e.RowIndex] .Cells [articlesPerWeekDataGridViewTextBoxColumn]值。);
十进制将val2 = VAL1 * 300;
INT basicCost = Convert.ToInt32(dataGridView1.Rows [e.RowIndex] .Cells [articlesPerWeekDataGridViewTextBoxColumn]值。);
INT calculatedBasicCost =(((basicCost * 3)* 52)/ 12);

十进制bonusPerArticle = Convert.ToDecimal(dataGridView1.Rows [e.RowIndex] .Cells [bonusDataGridViewTextBoxColumn]值。);
十进制maximumBonus =(((val1中* bonusPerArticle)* 52)/ 12);

十进制maximumLiability = calculatedBasicCost + maximumBonus;


dataGridView1.Rows [e.RowIndex] .Cells [ExpectedWeeklyWords]值= val2的。
dataGridView1.Rows [e.RowIndex] .Cells [CostOfContent]值= calculatedBasicCost;
dataGridView1.Rows [e.RowIndex] .Cells [MaximumBonus]值= maximumBonus。
dataGridView1.Rows [e.RowIndex] .Cells [MaximumLiability]值= maximumLiability。
}

}
}


解决方案

我设法制定出一个解决办法,但我仍然不确定为什么会发生。我最后不得不使用DataGridView中RowStateChanged事件和perfoming检查陈的状态更改为显示,然后在运行计算



使用代码如下:

 私人无效dataGridView1_RowStateChanged(对象发件人,DataGridViewRowStateChangedEventArgs E)
{
如果(e.StateChanged.ToString ()==显示)
{

DataClasses1DataContext getIsEmployed =新DataClasses1DataContext();
VAR的结果=从在getIsEmployed.writers
,其中a.name == dataGridView1.Rows [e.Row.Index] .Cells [nameDataGridViewTextBoxColumn]。Value.ToString()
选择一个;

的foreach(在结果VAR B)
{
如果(b.IsEmployed ==真)
{
INT VAL1 = Convert.ToInt32(dataGridView1 .Rows [e.Row.Index] .Cells [articlesPerWeekDataGridViewTextBoxColumn]值)。
十进制将val2 = VAL1 * 300;
十进制hourlyRate = Convert.ToDecimal(dataGridView1.Rows [e.Row.Index] .Cells [hourlyRateDataGridViewTextBoxColumn]值。);
INT contractedEmployedHours = Convert.ToInt32(dataGridView1.Rows [e.Row.Index] .Cells [hoursDataGridViewTextBoxColumn]值。);
十进制employedWage =(((hourlyRate * contractedEmployedHours)* 52)/ 12);

十进制employedBonusPerArticle = Convert.ToDecimal(dataGridView1.Rows [e.Row.Index] .Cells [bonusDataGridViewTextBoxColumn]值。);
十进制maximumEmployedBonus =(((val1中* employedBonusPerArticle)* 52)/ 12);

十进制maximumEmployedLiability = employedWage + maximumEmployedBonus;

dataGridView1.Rows [e.Row.Index] .Cells [ExpectedWeeklyWords]值= val2的。
dataGridView1.Rows [e.Row.Index] .Cells [CostOfContent]值= employedWage;
dataGridView1.Rows [e.Row.Index] .Cells [MaximumBonus]值= maximumEmployedBonus。
dataGridView1.Rows [e.Row.Index] .Cells [MaximumLiability]值= maximumEmployedLiability。
}
,否则
{
INT VAL1 = Convert.ToInt32(dataGridView1.Rows [e.Row.Index] .Cells [articlesPerWeekDataGridViewTextBoxColumn]值。);
十进制将val2 = VAL1 * 300;
INT basicCost = Convert.ToInt32(dataGridView1.Rows [e.Row.Index] .Cells [articlesPerWeekDataGridViewTextBoxColumn]值。);
INT calculatedBasicCost =(((basicCost * 3)* 52)/ 12);

十进制bonusPerArticle = Convert.ToDecimal(dataGridView1.Rows [e.Row.Index] .Cells [bonusDataGridViewTextBoxColumn]值。);
十进制maximumBonus =(((val1中* bonusPerArticle)* 52)/ 12);

十进制maximumLiability = calculatedBasicCost + maximumBonus;


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

}


Has anyone come across the issue that the RowsAdded event only fires twice and then leaves the loop?

I programatically am populating a DataGridView and then performing a couple of calculations to give appropriate figures.

My code is below but when run the DataGridView pulls and displays all the data but the calculated fields are only done for the first 2 rows. By using breakpoints this loops through correctly twice but then leaves the 3rd row and others without looping through the RowsAdded event.

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

        }
    }

解决方案

I managed to work out a workaround but I am still unsure why it happens. I ended up having to use the RowStateChanged event in DataGridview and perfoming a check to chen the state changes to "Displayed" and then running the calculations.

Code used is below:

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

    }

这篇关于在DataGridView中RowsAdded事件只发射了前两排的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆