I'm developing an application in Windows C # which takes the order inside a datagridview and when you press the "calculate total" button you have to add the "Cost" column. The problem is that I do not respect the decimals since it shows the whole number as an integer as the following image shows:
Instead of showing me 65.5, it shows me 655.
This is the code I use when I press the button:
private void button6_Click(object sender, EventArgs e)
{
for (int numColumna = 0; numColumna < dataGridView1.Columns.Count; numColumna++)
{
float sumTotal =0F;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
sumTotal += Convert.ToSingle(row.Cells[1].Value);
}
textBox1.Text = sumTotal.ToString();
}
}
Try to put it in the following way:
private void button6_Click(object sender, EventArgs e)
{
for (int numColumna = 0; numColumna < dataGridView1.Columns.Count; numColumna++)
{
var sumTotal ="";
foreach (DataGridViewRow row in dataGridView1.Rows)
{
sumTotal +=Convert.ToString(row.Cells[1].Value);
Convert.ToSingle(sumTotal);
MessageBox.Show(sumTotal);
}
MessageBox.Show(sumTotal);
textBox1.Text = sumTotal.ToString();
}
}
And what happens is that at the moment of doing the sum now it appears to me in the following way: