View Single Post
Join Date: Mar 2008
Posts: 268
Reputation: Traicey is an unknown quantity at this point 
Solved Threads: 19
Traicey's Avatar
Traicey Traicey is offline Offline
Posting Whiz in Training

Re: Master/Detail DropDownList and DataGrid

 
0
  #9
Dec 10th, 2008
My code is partial working cozI still have a problem coz when I select add to cart on my 1st datagrid it overwrite the existing record instead of adding to it but everything else is working

But then again u need to understand how the code work hey if u dont let me know then I will explain
  1. /*Filling up the dropdownlist with each category. I used a procedure which select everything from Category which only have CategoryID and categoryDescription*/
  2. protected void Page_Load(object sender, EventArgs e)
  3. {
  4. if (!Page.IsPostBack)
  5. {
  6. string strCategory = "sdp_ViewCategory";
  7. DataSet dsCategory = new DataSet();
  8. SqlDataAdapter daCategory = new SqlDataAdapter(strCategory, conn);
  9. daCategory.Fill(dsCategory, "Categories");
  10. ddlItems.DataSource = dsCategory;
  11. ddlItems.DataValueField = "CategoryID";
  12. ddlItems.DataTextField = "Description";
  13. ddlItems.DataBind();
  14. ddlItems.Items.Insert(0, "Please select a category");
  15.  
  16. }
  17. }
  18.  
  19. /*Selecting a category from dropdownlist(DDL) and desplay the details of all the available product on that category depending on what I have selected on the DDL*/
  20. protected void ddlItems_SelectedIndexChanged(object sender, EventArgs e)
  21. {
  22.  
  23. dgItems.Visible = true;
  24. string strItems = "sdp_ViewCartAndProduct";
  25.  
  26. SqlDataAdapter daItems = new SqlDataAdapter(strItems, conn);
  27. if (ddlItems.SelectedIndex > 0)
  28. {
  29. DataSet dsItems = new DataSet();
  30. daItems.SelectCommand.CommandText = "Select ProductID, ProductName, PictureName, UnitsInStock from dbo.Products where CategoryID = " +
  31. ddlItems.SelectedValue;
  32.  
  33.  
  34. try
  35. {
  36. dsItems.Tables.Remove("Products");
  37. }
  38. catch (Exception de)
  39. {
  40. lblMsg.Text = de.Message;
  41. }
  42. daItems.Fill(dsItems, "Products");
  43. dgItems.DataSource = dsItems;
  44. dgItems.DataMember = "Products";
  45. dgItems.DataBind();
  46. dgItems.Visible = true;
  47. //dgItems.SelectedItem.Cells[4].Visible = false;
  48. foreach(DataGridColumn col in dgItems.Columns)
  49. {
  50. if (col.HeaderText == "PictureName")
  51. col.Visible = false;
  52.  
  53. }
  54. }
  55. else
  56. dgItems.Visible = false;
  57. }
  58.  
  59. /*Im clicking button select which I added on my 1st datagrid and named it to add to cart then I add what the user select on the 1st datagrid using the select button*/
  60. protected void dgItems_SelectedIndexChanged(object sender, EventArgs e)
  61. {
  62.  
  63. imgItems.Visible = true;
  64. imgItems.ImageUrl = "Pictures/" + dgItems.SelectedItem.Cells[4].Text;
  65. string strDetails = "sdp_ViewProducts";
  66. DataSet dsDetails = new DataSet();
  67. SqlDataAdapter daDetails = new SqlDataAdapter(strDetails, conn);
  68. daDetails.SelectCommand.CommandText = "Select ProductID, ProductName, UnitPrice from dbo.Products where ProductID = " +
  69. dgItems.SelectedItem.Cells[2].Text;
  70.  
  71. daDetails.Fill(dsDetails, "Products");
  72. dgDetails.DataSource = dsDetails;
  73. dgDetails.DataMember = "Products";
  74. dgDetails.DataKeyField = "ProductID";
  75. dgDetails.DataBind();
  76. dgDetails.Visible = true;
  77. }
Last edited by Traicey; Dec 10th, 2008 at 3:15 am.
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote