I have 2 data grid views, one for Purchases and one for Sales
I sum the total of sales and subtract the total of purchases from it.
the problem is, it will only do the calculation if the number of rows of both datagridviews are the same, if one of them has more rows than the other, it will not give any value in the textbox where it should show the result
that's my code
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();
}