Adding Dynamic Rows in Gridview with dropdownlists in asp.net


/ Published in: VB.NET
Save to your folder(s)

how to dynamically add rows containing DropDownLists in ASP.Net GridView control


Copy this code and paste it in your HTML
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head runat="server">
  3. <title>Untitled Page</title>
  4. </head>
  5. <body>
  6. <form id="form1" runat="server">
  7. <div>
  8. <asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false">
  9. <Columns>
  10. <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
  11. <asp:TemplateField HeaderText="Header 1">
  12. <ItemTemplate>
  13. <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
  14. <asp:ListItem Value="-1">Select</asp:ListItem>
  15. </asp:DropDownList>
  16. </ItemTemplate>
  17. </asp:TemplateField>
  18. <asp:TemplateField HeaderText="Header 2">
  19. <ItemTemplate>
  20. <asp:DropDownList ID="DropDownList2" runat="server" AppendDataBoundItems="true">
  21. <asp:ListItem Value="-1">Select</asp:ListItem>
  22. </asp:DropDownList>
  23. </ItemTemplate>
  24. </asp:TemplateField>
  25. <asp:TemplateField HeaderText="Header 3">
  26. <ItemTemplate>
  27. <asp:DropDownList ID="DropDownList3" runat="server" AppendDataBoundItems="true">
  28. <asp:ListItem Value="-1">Select</asp:ListItem>
  29. </asp:DropDownList>
  30. </ItemTemplate>
  31. <FooterStyle HorizontalAlign="Right" />
  32. <FooterTemplate>
  33. <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row"
  34. onclick="ButtonAdd_Click" />
  35. </FooterTemplate>
  36. </asp:TemplateField>
  37. </Columns>
  38. </asp:gridview>
  39. </div>
  40. </form>
  41. </body>
  42. </html>
  43.  
  44.  
  45. private ArrayList GetDummyData()
  46. {
  47. ArrayList arr = new ArrayList();
  48. arr.Add(new ListItem("Item1", "1"));
  49. arr.Add(new ListItem("Item2", "2"));
  50. arr.Add(new ListItem("Item3", "3"));
  51. arr.Add(new ListItem("Item4", "4"));
  52. arr.Add(new ListItem("Item5", "5"));
  53. return arr;
  54. }
  55.  
  56. private void FillDropDownList(DropDownList ddl)
  57. {
  58. ArrayList arr = GetDummyData();
  59. foreach (ListItem item in arr)
  60. {
  61. ddl.Items.Add(item);
  62. }
  63. }
  64.  
  65. private void SetInitialRow()
  66. {
  67. DataTable dt = new DataTable();
  68. DataRow dr = null;
  69.  
  70. //Define the Columns
  71. dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
  72. dt.Columns.Add(new DataColumn("Column1", typeof(string)));
  73. dt.Columns.Add(new DataColumn("Column2", typeof(string)));
  74. dt.Columns.Add(new DataColumn("Column3", typeof(string)));
  75.  
  76. //Add a Dummy Data on Initial Load
  77. dr = dt.NewRow();
  78. dr["RowNumber"] = 1;
  79. dt.Rows.Add(dr);
  80.  
  81. //Store the DataTable in ViewState
  82. ViewState["CurrentTable"] = dt;
  83. //Bind the DataTable to the Grid
  84. Gridview1.DataSource = dt;
  85. Gridview1.DataBind();
  86.  
  87. //Extract and Fill the DropDownList with Data
  88. DropDownList ddl1 = (DropDownList)Gridview1.Rows[0].Cells[1].FindControl("DropDownList1");
  89. DropDownList ddl2 = (DropDownList)Gridview1.Rows[0].Cells[2].FindControl("DropDownList2");
  90. DropDownList ddl3 = (DropDownList)Gridview1.Rows[0].Cells[3].FindControl("DropDownList3");
  91. FillDropDownList(ddl1);
  92. FillDropDownList(ddl2);
  93. FillDropDownList(ddl3);
  94.  
  95. }
  96. private void AddNewRowToGrid()
  97. {
  98.  
  99. if (ViewState["CurrentTable"] != null)
  100. {
  101. DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
  102. DataRow drCurrentRow = null;
  103.  
  104. if (dtCurrentTable.Rows.Count > 0)
  105. {
  106. drCurrentRow = dtCurrentTable.NewRow();
  107. drCurrentRow["RowNumber"] = dtCurrentTable.Rows.Count + 1;
  108. //add new row to DataTable
  109. dtCurrentTable.Rows.Add(drCurrentRow);
  110. //Store the current data to ViewState
  111. ViewState["CurrentTable"] = dtCurrentTable;
  112.  
  113. for (int i = 0 ; i < dtCurrentTable.Rows.Count - 1; i++)
  114. {
  115. //extract the DropDownList Selected Items
  116. DropDownList ddl1 = (DropDownList)Gridview1.Rows[i].Cells[1].FindControl("DropDownList1");
  117. DropDownList ddl2 = (DropDownList)Gridview1.Rows[i].Cells[2].FindControl("DropDownList2");
  118. DropDownList ddl3 = (DropDownList)Gridview1.Rows[i].Cells[3].FindControl("DropDownList3");
  119.  
  120. // Update the DataRow with the DDL Selected Items
  121. dtCurrentTable.Rows[i]["Column1"] = ddl1.SelectedItem.Text;
  122. dtCurrentTable.Rows[i]["Column2"] = ddl2.SelectedItem.Text;
  123. dtCurrentTable.Rows[i]["Column3"] = ddl3.SelectedItem.Text;
  124.  
  125. }
  126.  
  127. //Rebind the Grid with the current data
  128. Gridview1.DataSource = dtCurrentTable;
  129. Gridview1.DataBind();
  130. }
  131. }
  132. else
  133. {
  134. Response.Write("ViewState is null");
  135. }
  136.  
  137. //Set Previous Data on Postbacks
  138. SetPreviousData();
  139. }
  140.  
  141. private void SetPreviousData()
  142. {
  143. int rowIndex = 0;
  144. if (ViewState["CurrentTable"] != null)
  145. {
  146. DataTable dt = (DataTable)ViewState["CurrentTable"];
  147. if (dt.Rows.Count > 0)
  148. {
  149. for (int i = 0; i < dt.Rows.Count; i++)
  150. {
  151. //Set the Previous Selected Items on Each DropDownList on Postbacks
  152. DropDownList ddl1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
  153. DropDownList ddl2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DropDownList2");
  154. DropDownList ddl3 = (DropDownList)Gridview1.Rows[rowIndex].Cells[3].FindControl("DropDownList3");
  155.  
  156. //Fill the DropDownList with Data
  157. FillDropDownList(ddl1);
  158. FillDropDownList(ddl2);
  159. FillDropDownList(ddl3);
  160.  
  161. if (i < dt.Rows.Count - 1)
  162. {
  163. ddl1.ClearSelection();
  164. ddl1.Items.FindByText(dt.Rows[i]["Column1"].ToString()).Selected = true;
  165.  
  166. ddl2.ClearSelection();
  167. ddl2.Items.FindByText(dt.Rows[i]["Column2"].ToString()).Selected = true;
  168.  
  169. ddl3.ClearSelection();
  170. ddl3.Items.FindByText(dt.Rows[i]["Column3"].ToString()).Selected = true;
  171. }
  172.  
  173. rowIndex++;
  174. }
  175. }
  176. }
  177. }
  178.  
  179. protected void Page_Load(object sender, EventArgs e)
  180. {
  181. if (!Page.IsPostBack)
  182. {
  183. SetInitialRow();
  184. }
  185. }
  186. protected void ButtonAdd_Click(object sender, EventArgs e)
  187. {
  188. AddNewRowToGrid();
  189. }

URL: http://www.aspsnippets.com/Articles/Adding-Dynamic-Rows-in-GridView-with-DropDownLists-in-ASP.Net.aspx

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.