有没有人遇到过RowsAdded
事件只触发两次然后离开循环的问题?
我以编程方式填充 aDataGridView
然后执行一些计算以给出适当的数字。
我的代码在下面,但是当运行DataGridView
拉取并显示所有数据时,计算字段仅针对前 2 行完成。通过使用断点,这会正确循环两次,但随后会离开第 3 行和其他行而不循环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;
}
}
}