Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I couldn't get the sum total of rows at the footer of datagridview

my table is

id    purticulars     debit    credit 
1      ammu           200        0
2     chinnu          300        0
3      annie           0        500
4      mittu           0        500



I want output as
id    purticulars     debit    credit 
1      ammu           200        0
2     chinnu          300        0
3      annie           0        500
4      mittu           0        500
-----------------------------------------
       total          500       1000


What I have tried:

I have tried this

C#
for (int m = 1; m < dt.Columns.Count; m++)
                {
                    double sum = 0;
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        sum += Convert.ToDouble(dt.Rows[j][m]);
                        dataGridView1.Rows[dt.Rows.Count].Cells[m + 1].Value = sum.ToString();
                    }



But it shows error as input string is not in correct format
Posted
Updated 2-Aug-18 20:14pm
v2

1 solution

Some of the cells you are iterating do not contain a value which can be converted to double. Debug the for loop you have to identify the issue. You should also consider using TryParse for conversion.

You are looping from second column (index 1) which from your post is not having numbers
 
Share this answer
 
v2
Comments
Member 13854008 3-Aug-18 3:15am    
thanku sir..i got it
i have one doubt too sir. The word "TOTAL" also want at end of row of cell 1

i have tried below code
for (int i = 1; i < dt.Columns.Count; i++)
{
dataGridView1[i, 1].Value = "TOTAL";

}

it didn't gave crct answer. When i had work this , the full cell 1 written as total.
but i want total came at the last row of cell 1

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