hello.

i m using a datagridview in c#.

i wana calculate total cost value from multiple rows of a single column in a data grid.

i m actually sending the rows of the grid to the datatable and then counts the value throw

foreach(datarow in dt.rows.count)
{


}

but it adds an extra value of the datatable eg 3, while total rows are eg 2 in the datatable.

hope u will understand the problem.

Hi,

I am not sure I understand what you are doing here:

i m actually sending the rows of the grid to the datatable and then counts the value throw

But here is the code to total a column directly in the gridview:

int total = 0;

            foreach (DataGridViewRow dgvr in dataGridView1.Rows)
            {
                total += Convert.ToInt16(dgvr.Cells[0].Value);
            }

            MessageBox.Show(total.ToString());

If this is not what you are looking for then please post some more code and I will try and help you.

Regards

Thanks dear.

you have solved my problem.

i hope u will help me in future too.

thx and regards.

No problem. Could you please mark this thre as solved please.

Regards

Paul

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.