2

i am using Telerik tools in Win forms i couldn't get sum of column in rad text box know how to do in normal grid view any suggestion pleas ?

 int sum = 0;
    for (int i = 0; i < radGridView1.Rows.Count-1; ++i)
    {
       sum += int.Parse(radGridView1.Rows[i].Cells[5].Value.ToString());
    }
    radTextBox1.Text  = sum.ToString();
Franco Rondini
  • 10,841
  • 8
  • 51
  • 77
  • I don't know what language is that, but you are converting an `int` into `string` and then it into `int` again. Is this correct, and if so why? – Lucio Jan 20 '14 at 16:00
  • c# --- i convert grid cell value to INT –  Jan 20 '14 at 16:02

2 Answers2

0

I have tested this approach and it works fine on my end with RadGridView, as long as Column[5] has valid numeric values:

        int sum = 0;
        for (int i = 0; i < radGridView1.Rows.Count - 1; ++i)
        {
            sum += int.Parse(radGridView1.Rows[i].Cells[0].Value.ToString());
        }
        this.Text = sum.ToString();

Also, you can consider the summary rows functionality for the purpose: http://www.telerik.com/help/winforms/gridview-rows-summary-rows.html

checho
  • 3,092
  • 3
  • 18
  • 30
  • Yes, as long ... I would rather use TryParse http://msdn.microsoft.com/en-gb/library/vstudio/f02979c7.aspx – DatRid Jan 20 '14 at 16:20
0

Instead of using column index, why not use column name?

From sum += int.Parse(radGridView1.Rows[i].Cells[0].Value.ToString());

To sum += int.Parse(radGridView1.Rows[i].Cells["ColumnName"].Value.ToString());