Karthik K

Karthik K

  • 975
  • 429
  • 45k

Gridview Grouping/Subtotal ?

Mar 20 2019 8:12 PM
Hi Friends ,
 
I have done Gridview grouping , In that i want to do subtotal as well as Grandtotal of it .I coulud share the code .if you suggest me with code .
 
Design :
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head runat="server">  
  3. <title></title>  
  4. </head>  
  5. <body>  
  6. <form id="form1" runat="server">  
  7. <div align="center">  
  8. <table style="border:solid 15px Grey; width: 80%; vertical-align: central;">  
  9. <tr>  
  10. <td style="padding-left: 20px; padding-top: 20px; padding-bottom: 20px; background-color: skyblue; font-family: 'Times New Roman'; font-size: 20pt; color: red;">Showing Grouping Of Data In a ASP.NET Grid View - Cloud Live  
  11. </td>  
  12. </tr>  
  13. <tr>  
  14. <td style="text-align: left;">  
  15. <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="Both" Width="90%">  
  16. <AlternatingRowStyle BackColor="White" ForeColor="#284775" />  
  17. <EditRowStyle BackColor="#999999" />  
  18. <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  19. <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  20. <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />  
  21. <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />  
  22. <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />  
  23. <SortedAscendingCellStyle BackColor="#E9E7E2" />  
  24. <SortedAscendingHeaderStyle BackColor="#506C8C" />  
  25. <SortedDescendingCellStyle BackColor="#FFFDF8" />  
  26. <SortedDescendingHeaderStyle BackColor="#6F8DAE" />  
  27. </asp:GridView>  
  28. </td>  
  29. </tr>  
  30. </table>  
  31. </div>  
  32. </form>  
  33. </body>  
  34. </html>  
backend :
  1. private void BindGrid()  
  2. {  
  3. MySqlConnection Cn = new MySqlConnection(Constring);  
  4. ds = new DataSet();  
  5. MySqlCommand cmd = new MySqlCommand("SELECT concat(DATE_FORMAT(a.transtime,'%h:%00 %p'),'-',DATE_FORMAT(a.transtime,'%h:59 %p')) as Hour,B.BarCode,Sum(b.qty) as Qty, B.InventoryDesc, Sum(b.PSDiscAmount+b.ManualDisc+b.MemberDisc+b.TotalDisc) as Disamt, Sum(B.TaxableAmount+B.TaxAmount1+B.TaxAmount2+B.TaxAmount3+B.TaxAmount4+B.TaxAmount5+B.TaxAmount6) As Amount FROM rasa1.Trans A JOIN rasa1.TransDetail B on A.Transid=B.Transid Left Join rasa1.Customers C On a.CustomerNumber=C.CustomerNumber Where (A.BusinessDate Between '2019-03-20' And '2019-03-20') And B.ItemStatus<>1 Group by Hour,Barcode,CategoryNumber ORDER BY HOUR(a.TransTime)", Cn);  
  6. da = new MySqlDataAdapter(cmd);  
  7. da.Fill(ds);  
  8. Cn.Open();  
  9. cmd.ExecuteNonQuery();  
  10. Cn.Close();  
  11. if (ds.Tables[0].Rows.Count > 0)  
  12. {  
  13. GridView1.DataSource = ds.Tables[0];  
  14. GridView1.DataBind();  
  15. // First parameter is Row Collection  
  16. // Second is Start Column  
  17. // Third is total number of Columns to make group of Data.  
  18. ShowingGroupingDataInGridView(GridView1.Rows, 0, 2);  
  19. }  
  20. }  
  21. Groupby Function :  
  22. void ShowingGroupingDataInGridView(GridViewRowCollection gridViewRows, int startIndex, int totalColumns)  
  23. {  
  24. if (totalColumns == 0) return;  
  25. int i, count = 1;  
  26. ArrayList lst = new ArrayList();  
  27. lst.Add(gridViewRows[0]);  
  28. var ctrl = gridViewRows[0].Cells[startIndex];  
  29. for (i = 1; i < gridViewRows.Count; i++)  
  30. {  
  31. TableCell nextTbCell = gridViewRows[i].Cells[startIndex];  
  32. if (ctrl.Text == nextTbCell.Text)  
  33. {  
  34. count++;  
  35. nextTbCell.Visible = false;  
  36. lst.Add(gridViewRows[i]);  
  37. }  
  38. else  
  39. {  
  40. if (count > 1)  
  41. {  
  42. ctrl.RowSpan = count;  
  43. ShowingGroupingDataInGridView(new GridViewRowCollection(lst), startIndex + 1, totalColumns - 1);  
  44. }  
  45. count = 1;  
  46. lst.Clear();  
  47. ctrl = gridViewRows[i].Cells[startIndex];  
  48. lst.Add(gridViewRows[i]);  
  49. }  
  50. }  
  51. if (count > 1)  
  52. {  
  53. ctrl.RowSpan = count;  
  54. ShowingGroupingDataInGridView(new GridViewRowCollection(lst), startIndex + 1, totalColumns - 1);  
  55. }  
  56. count = 1;  
  57. lst.Clear();  
  58. }  
Thanking You ,

Answers (1)