I am using the below code to get the sum of a column in a data grid view.
private void button16_Click(object sender, EventArgs e)
{
int sum = 0;
for (int i = 0; i < dataGridView4.Rows.Count; ++i)
{
sum += Convert.ToInt32(dataGridView4.Rows[i].Cells[10].Value);
}
try
{
decimal tot = 0;
for (int i=0; i <= dataGridView4.RowCount -1; i++)
{
tot += Convert.ToDecimal (dataGridView4.Rows[i].Cells[10].Value);
}
if (tot==0) {}
textBox34.Text = tot.ToString();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
I am getting the error message
Input string was not in a correct format.
ِI found that the problem is in the formatting. Becasue the SQL server data type for that column is money. and the SQL server changes any number I save to this format. 00.0000 For example, If I save 10 SQL server saves it as 10.0000
If I remove the (.) I get no errors.
If I try to sum 10.0000 + 3.0000 it never works out.
Any ideas?