لطفا این رو تست کنید
int M = 0;
dataGridView1.Rows.Cast<DataGridViewRow>().Where(c 2 => !c2.Cells[3].Value.ToString().StartsWith("کالای"))
.Where(c3 => !c3.Cells[3].Value.ToString().StartsWith("تخفیف"))
.All(c =>
{
M = dataGridView1.Rows.Cast<DataGridViewRow>()
.Where(c1 => c1.Index < c.Index)
.Where(c1 => !c1.Cells[3].Value.ToString().StartsWith("کالای"))
.Select(c1 => int.Parse(c1.Cells[8].Value.ToString()) -
int.Parse(c1.Cells[9].Value.ToString())).Sum() +
int.Parse(c.Cells[8].Value.ToString()) -
int.Parse(c.Cells[9].Value.ToString());
c.Cells[11].Value = M;
c.Cells[10].Value = M == 0 ? "تسویه" : (M > 0 ? "بدهکار" : "بستانکار");
return true;
});





پاسخ با نقل قول