Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Guys, i want to show sum values in last cell of each row
http://imgur.com/a/9qcKx

What I have tried:

DataSet dsa = new DataSet();
DataTable dt1 = new DataTable();
dsa.Tables.Add(dt1);
OleDbDataAdapter da = new OleDbDataAdapter();
da = new OleDbDataAdapter("SELECT column2," +
"Sum(MediumVal) As [Dine In], " +
"Sum(LargeVal) As [Deliery], " +
"Sum(RoyalVal) As [Take Away] " +
"From ( " +
"SELECT " +
"column2 As [column2], " +
"Switch(column3=\"Table1/Table\" OR column3=\"Table2/Table\"OR column3=\"Table3/Table\"OR column3=\"Table4/Table\"OR column3=\"Table5/Table\"OR column3=\"Table6/Table\"OR column3=\"Table7/Table\"OR column3=\"Table8/Table\"OR column3=\"Table9/Table\"OR column3=\"Table10/Table\"OR column3=\"Table11/Table\"OR column3=\"Table12/Table\"OR column3=\"Table13/Table\"OR column3=\"Table14/Table\"OR column3=\"Table15/Table\"OR column3=\"Table16/Table\"OR column3=\"Table17/Table\"OR column3=\"Table18/Table\"OR column3=\"Table19/Table\"OR column3=\"Table20/Table\", 1,True,0) As [MediumVal], " +
"Switch(column3=\"Table1/Delivery\"OR column3=\"Table2/Delivery\"OR column3=\"Table3/Delivery\"OR column3=\"Table4/Delivery\"OR column3=\"Table5/Delivery\"OR column3=\"Table6/Delivery\"OR column3=\"Table7/Delivery\"OR column3=\"Table8/Delivery\"OR column3=\"Table9/Delivery\"OR column3=\"Table10/Delivery\"OR column3=\"Table11/Delivery\"OR column3=\"Table12/Delivery\"OR column3=\"Table13/Delivery\"OR column3=\"Table14/Delivery\"OR column3=\"Table15/Delivery\"OR column3=\"Table16/Delivery\"OR column3=\"Table17/Delivery\"OR column3=\"Table18/Delivery\"OR column3=\"Table19/Delivery\"OR column3=\"Table20/Delivery\", 1,True,0) As [LargeVal], " +
"Switch(column3=\"Take Away\", 1,True,0) As [RoyalVal] " +
"FROM Total " +
" Where [Date] between #" + dateTimePicker1.Value.ToString("dd/MM/yyyy") +
"# AND #" + dateTimePicker2.Value.ToString("dd/MM/yyyy") + "# AND [column2] IN('Small.......','Medium......','Large.......','Ex Large....')" +
" ) Group By column2", VCON);
da.Fill(dt1);
dataGridView1.DataSource = dt1;
VCON.Close();
dataGridView1.Columns[0].Width = 286;
dataGridView1.Columns[1].Width = 180;
dataGridView1.Columns[2].Width = 180;
dataGridView1.Columns[3].Width = 180;
dataGridView1.Columns[4].Width = 180;
Posted
Updated 2-Oct-16 2:07am
v2

0) Create a model for your items
1) Load the results of the database query into a list of model items
2) Create a new model item that contains the desired summed fields (use LINQ to get your sums).
3) And the new model item to the list
4) Bind the list to the dataGridView control.
 
Share this answer
 
try like this

C#
DataTable dt = new DataTable();
           dt.Columns.Add("Size");
           dt.Columns.Add("Dine In");
           dt.Columns.Add("Delivery");
           dt.Columns.Add("Take Away");
           dt.Rows.Add("large", 12, 2, 0);
           dt.Rows.Add("Medium", 6, 1, 0);
           dt.Rows.Add("Small", 6, 1, 0);
           dataGridView1.DataSource = dt;
           for (int i = 1; i < dt.Columns.Count; i++)
           {
               double sum = 0;
               for (int j = 0; j < dt.Rows.Count; j++)
               {
                   sum += Convert.ToDouble(dt.Rows[j][i]);
               }
               dataGridView1.Rows[dt.Rows.Count].Cells[i].Value = sum.ToString();
           }
 
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