Click here to Skip to main content
15,886,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Member,
i have a problem.
in my datagridview a column "amount" i want total of sum of all values in that coluns.
but main problem is that suppose there is 3 rows and there values are 100,200,400 and my curser in 400 columns then total values is 300.

Means i want values when columns values change but my curser in that columns..


Thans you.
Posted
Comments
Al Moje 10-Feb-12 2:03am    
Could you show us your code on how you arrive such error so that we could analyze...
Aniket Yadav 10-Feb-12 2:10am    
Where do you want the total to be displayed. i mean to say that whether in another textbox or in the grid itself

C#
private decimal[] d;
d = new decimal[31];

for(int count = 0; count < myVariable; count++)
{
     d[count] = Convert.ToDecimal(myGrid.Rows[e.RowIndex].Cells[count].Value);
}

myGrid.Rows[e.RowIndex].Cells.Value = d[0] + d[29]; //for example.

I added this in CellEndEdit event.


Or Try This

C#
decimal total = 0M;
foreach(DataGridViewRow row in datagridview1.Rows)
{
      decimal temp = 0M;
      if(Decimal.TryParse(dataGridView[0, row.Index].Value.ToString(), out temp)) // 0 is 1st column - set your column index!
           total += temp;
}
//set result of total in last row:
datagridview1[0, datagridview1.Rows.Count -1].Value = total.ToString();


Or See This Link

http://www.timvw.be/2007/01/04/developing-a-datasource-for-your-datagridview/[^]

Accept This Answer If It Has Helped You
 
Share this answer
 
v2
 
Share this answer
 
Comments
Espen Harlinn 11-Feb-12 5:18am    
5'ed!
Abhinav S 11-Feb-12 8:39am    
Thank you Espen.

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