我有2个数据网格视图,一个用于购买,一个用于销售
我将销售总额相加并减去购买总额。
问题是,如果两个datagridviews的行数相同,它只会进行计算,如果其中一个行的行数多于另一个,它将不会在文本框中给出应该显示的任何值结果
这是我的代码
private void GrossSale()
{
int sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);
}
txtGrossSale.Text = sum.ToString();
}
private void NetSale()
{
int sum2 = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum2 += Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value);
}
txtNetSale.Text = sum2.ToString();
}
private void GrossPurchase()
{
int sum3 = 0;
for (int i = 0; i < dataGridView2.Rows.Count; ++i)
{
sum3 += Convert.ToInt32(dataGridView2.Rows[i].Cells[2].Value);
}
txtGrossPurchase.Text = sum3.ToString();
}
private void NetPurchase()
{
int sum4 = 0;
for (int i = 0; i < dataGridView2.Rows.Count; ++i)
{
sum4 += Convert.ToInt32(dataGridView2.Rows[i].Cells[3].Value);
}
txtNetPurchase.Text = sum4.ToString();
}
private void TotalGross()
{
int sum5 = 0;
int sum6 = 0;
int sum7 = 0;
for (int i = 0; i < dataGridView2.Rows.Count; ++i)
{
sum5 += Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value);
sum6 += Convert.ToInt32(dataGridView2.Rows[i].Cells[3].Value);
}
sum7 = sum5 + sum6;
txtGross.Text = sum7.ToString();
}
答案 0 :(得分:2)
看看TotalGross()。您正在循环遍历 dataGridView2 .Rows.Count,但正在循环中访问 dataGridView1 .Rows [i] AND dataGridView2.Rows [i] ...