Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In Dynamic Grid View Adding Sum of Column value in Footer


firstTable is Data table
And grid view Like

No	Job Description	Total MD Used MD Balance MD	 
1	Blk-66A  	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 
1.1	1st story 	3.00	3.00	0.00	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 
2	Blk-66A 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 
2.1	Basement  	4.00	2.00	2.00	 	 	 	 	  	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 
2.2	Basement  	3.00	1.00	2.00	 
----------------------------------------------
     Total	 	 	 


Specified cast is not valid. as Error
OUT PUT Like in Gridview

<pre>No	Job Description	Total MD Used MD Balance MD	 
1	Blk-66A  	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 
1.1	1st story 	3.00	3.00	0.00	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 
2	Blk-66A 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 
2.1	Basement  	4.00	2.00	2.00	 	 	 	 	  	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 
2.2	Basement  	3.00	1.00	2.00	 
----------------------------------------------
     Total	    10.00    5.00    4.00


What I have tried:

C#

Decimal total = 0; ;
              leaveGrid.FooterRow.Cells[0].Text = "Total";
              leaveGrid.FooterRow.Cells[1].Font.Bold = true;
              leaveGrid.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Left;
              for (int k = 2; k < firstTable.Columns.Count; k++)
              {

                  total = firstTable.AsEnumerable().Where(row => row.Field<String>(firstTable.Columns[k].ToString()) != "").Sum(row => row.Field<Decimal>(firstTable.Columns[k].ToString()));
                  leaveGrid.FooterRow.Cells[k].Text = total.ToString();
                  leaveGrid.FooterRow.Cells[k].Font.Bold = true;
                  leaveGrid.FooterRow.BackColor = System.Drawing.Color.Beige;
             }
Posted
Updated 29-Dec-21 10:25am
v4
Comments
OriginalGriff 25-Dec-21 1:47am    
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.

1 solution

If uou're using DataTable object as a DataSource of DataGridView property, use something like this:
total = firstTable.AsEnumerable().Where(row => !DBNull.Value.Equals(row[k])).Sum(row => Convert.ToDecimal(row[k]));
 
Share this answer
 

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