public void sum1()
{
try
{
int sum = 0;
foreach(Row r in dataGridView1.Rows)
{
string text = r.Cells[1].Text;
string value = r.Cells[2].Text;
if (text.Contains("C"))
{
sum += Convert.ToInt32(value);
}
else if (text.contains("D"))
{
sum -= Convert.ToInt32(value);
}
}
string suffix = string.empty;
if(sum>0){suffix = "cr"}
else if(sum<0){suffix = "dr"}
txttotalsum.Text = sum.ToString() + suffix;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
i suggest u using the foreach loop, is safier than the for loop.
about the zero-nonzero suffix you can decide it whith an if-cascade
in that case there are cr for positive, dr for negative and nothing for zero.
it depends on what you prefer