| 0. | ... |
| 1. | using System.Data.OleDb; |
| 2. | |
| 3. | namespace MyNamespace |
| 4. | { |
| 5. | | public class MyWebFormPage : System.Web.UI.Page |
| 6. | | { |
| 7. | | | protected Webmodelling.Controls.Menues.SlidingMenu smFood; |
| 8. | | | private System.Data.OleDb.OleDbDataAdapter oleDA; |
| 9. | |
| 10. | | | private void Page_Load(object sender, System.EventArgs e) |
| 11. | | | { |
| 12. | | | | //delegating the GroupDataBound event using the SlidingMenuGroupEventHandler |
| 13. | | | | smFood.GroupDataBound += new Webmodelling.Controls.Menues.SlidingMenuGroupEventHandler(smFood_GroupDataBound); |
| 14. | |
| 15. | | | | //getting a physical file reference to the Access database, Northwind |
| 16. | | | | string appl_physical_path = Request.PhysicalApplicationPath; |
| 17. | | | | if (!appl_physical_path.EndsWith("\\")){appl_physical_path = appl_physical_path + "\\";} //old habit, sorry |
| 18. | | | | string physicalPath_Northwind = appl_physical_path + @"Data\Northwind.mdb"; |
| 19. | |
| 20. | | | | //establishing a Connection object (here an OleDbConnection) |
| 21. | | | | string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + physicalPath_Northwind; |
| 22. | | | | string sqlCategories = "SELECT CategoryID, CategoryName, Description FROM Categories"; |
| 23. | | | | OleDbConnection objConn = new OleDbConnection(strConn); |
| 24. | | | | |
| 25. | | | | //filling a (disconnected) DataTable from the data store |
| 26. | | | | OleDbCommand objComm = new OleDbCommand(); |
| 27. | | | | objComm.Connection = objConn; |
| 28. | | | | objComm.CommandText = sqlCategories; |
| 29. | | | | oleDA = new OleDbDataAdapter(); |
| 30. | | | | oleDA.SelectCommand = objComm; |
| 31. | | | | DataTable dtCategories = new DataTable(); |
| 32. | | | | oleDA.SelectCommand.Connection.Open(); |
| 33. | | | | oleDA.Fill(dtCategories); //fill the DataTable (at this moment, there need to be an open connection to the data store) |
| 34. | | | | oleDA.SelectCommand.Connection.Close(); |
| 35. | |
| 36. | | | | //setting the DataSource of the SlidingMenu, smFood |
| 37. | | | | smFood.DataSource = dtCategories.Rows; //the Rows property of a DataTable is a DataRowCollection, a DataRowCollection implements IEnumerable |
| 38. | | | | smFood.DataBind(); |
| 39. | | | } |
| 40. | |
| 41. | | | protected void smFood_GroupDataBound(object sender, Webmodelling.Controls.Menues.SlidingMenuGroupEventArgse) |
| 42. | | | { |
| 43. | | | | DataRowCollection dataSource = (DataRowCollection)smFood.DataSource; |
| 44. | | | | //DataRowCollection dataSource = (DataRowCollection)((Webmodelling.Controls.Menues.DataBound)sender).DataSource; |
| 45. | | | | Label lblHeader = (Label)e.GroupHeader.FindControl("lblHeader"); |
| 46. | | | | Label lblDescription = (Label)e.GroupCanvas.FindControl("lblDescription"); |
| 47. | | | | DataGrid dgProduct = (DataGrid)e.GroupCanvas.FindControl("dgProduct"); |
| 48. | | | | |
| 49. | | | | lblHeader.Text = (string)dataSource[e.GroupIndex]["CategoryName"]; |
| 50. | | | | lblDescription.Text = (string)dataSource[e.GroupIndex]["Description"]; |
| 51. | |
| 52. | | | | //the DataSource of the DataGrid can take a DataTable |
| 53. | | | | dgProduct.DataSource = Product((int)dataSource[e.GroupIndex]["CategoryID"]); |
| 54. | | | | dgProduct.DataBind(); |
| 55. | |
| 56. | | | | if (e.GroupIndex % 2 != 0) |
| 57. | | | | { |
| 58. | | | | | lblHeader.ForeColor = System.Drawing.ColorTranslator.FromHtml("#3135ce"); |
| 59. | | | | | lblDescription.ForeColor = System.Drawing.ColorTranslator.FromHtml("#3135ce"); |
| 60. | | | | | e.GroupHeader.GroupHeaderStyles.BackgroundColor = "#ce9c31"; |
| 61. | | | | | e.GroupHeader.GroupHeaderStyles.BackgroundImage = "/images/backgrounds/background_tube_braun_height20.jpg"; |
| 62. | | | | | e.GroupCanvas.GroupCanvasStyles.BackgroundColor = "#ce9c31"; |
| 63. | | | | | e.GroupCanvas.GroupCanvasStyles.ScrollbarFaceColor = "#ce9c31"; |
| 64. | | | | | dgProduct.ForeColor = System.Drawing.ColorTranslator.FromHtml("#ffffff"); |
| 65. | | | | | dgProduct.HeaderStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#ce9c31"); |
| 66. | | | | | dgProduct.BackColor = System.Drawing.ColorTranslator.FromHtml("#ce9c31"); |
| 67. | | | | } |
| 68. | |
| 69. | | | | if ((string)dataSource[e.GroupIndex]["CategoryName"] == "Grains/Cereals") |
| 70. | | | | { |
| 71. | | | | | e.GroupHeader.GroupHeaderStyles.OpenThisGroupAtStartup = true; |
| 72. | | | | } |
| 73. | | | } |
| 74. | |
| 75. | | | //returning those products that belongs to the current category |
| 76. | | | private DataTable Product(int CategoryID) |
| 77. | | | { |
| 78. | | | | DataTable dt = new DataTable(); |
| 79. | | | | string sqlProducts = "SELECT ProductName, UnitPrice FROM Products WHERE CategoryID = " + CategoryID; |
| 80. | | | | oleDA.SelectCommand.CommandText = sqlProducts; |
| 81. | | | | oleDA.SelectCommand.Connection.Open(); |
| 82. | | | | oleDA.Fill(dt); |
| 83. | | | | oleDA.SelectCommand.Connection.Close(); |
| 84. | | | | return dt; |
| 85. | | | } |
| 86. | | } |
| 87. | } |