Click here to Skip to main content
16,004,778 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
C#
Int32 sum = 0;
             for (Int32 i = 0 ; i< this.GridView1.Rows.Count; i++)
             {
                 sum += Int32.Parse(GridView1.Rows[i].Cells[4].Text);

             }

how to add the values of a column???
Posted
Comments
[no name] 24-Sep-13 7:19am    
Just like you are?
Omprakash Kukana 24-Sep-13 7:37am    
wt u mean?
Omprakash Kukana 24-Sep-13 7:37am    
its not working
Omprakash Kukana 24-Sep-13 7:40am    
Int32.Parse(GridView1.Rows[i].Cells[4].Text);
here error is input string was not in a correct format
[no name] 24-Sep-13 7:53am    
Don't you think that the error message that you are getting is kind of "need to know information" for anyone that might try and help you? The error message means exactly what it says, the text cannot be parsed to an integer.

1 solution

C#
decimal a = 0;
           for (Int32 i = 0; i < this.GridView1.Rows.Count; i++)
           {

               a = a + Convert.ToDecimal(((Label)GridView1.Rows[i].FindControl("label3")).Text.ToString());

           }
           lbl_total.Text = Convert.ToString(a);
 
Share this answer
 
Comments
Richard MacCutchan 25-Sep-13 7:54am    
That's a very bad way of doing it. What if the text contains non-numeric characters; you will just get an exception.
[no name] 25-Sep-13 9:57am    
He's rep building.
Richard MacCutchan 25-Sep-13 10:13am    
Thought so, what a waste of time and effort.
Omprakash Kukana 26-Sep-13 0:13am    
if this solution is not a better way to solve problem then tell me your solution
Richard MacCutchan 26-Sep-13 3:22am    
Look at your Convert statement. Firstly why are you calling ToString on a Text item, which is already a string? What happens if the text contains invalid characters, what happens if the call to FindControl fails, etc. You should not assume that all these parts of the statement will succeed. And lastly you should not use Convert unless you can guarantee that your string will contain only digits; use TryParse, and be ready to handle error conditions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900