Debasis Mohapatra

Debasis Mohapatra

  • NA
  • 381
  • 57.1k

Dynamically Adding and Deleting Rows in GridView and Saving

Dec 16 2019 4:26 AM
Hi,
 
After click the Addnew button in Gridview the data is adding . i have a drop down box after that when i have enter the Dynamically Adding the rows the data is adding now the question is how to save the data in after click the save button.
 
My code
  1. private void SetPreviousData()  
  2. {  
  3. int rowIndex = 0;  
  4. if (ViewState["CurrentTable"] != null)  
  5. {  
  6. DataTable dt = (DataTable)ViewState["CurrentTable"];  
  7. if (dt.Rows.Count > 0)  
  8. {  
  9. for (int i = 0; i < dt.Rows.Count; i++)  
  10. {  
  11. DropDownList box1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("ddlhead");  
  12. TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txtamount");  
  13. TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txtremark");  
  14. box1.Text = dt.Rows[i]["Column1"].ToString();  
  15. box2.Text = dt.Rows[i]["Column2"].ToString();  
  16. box3.Text = dt.Rows[i]["Column3"].ToString();  
  17. rowIndex++;  
  18. }  
  19. }  
  20. }  
  21. protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)  
  22. {  
  23. }  
  24. protected void btnsave_Click(object sender, EventArgs e)  
  25. {  
  26. string sql = string.Empty;  
  27. try  
  28. {  
  29. if (string.IsNullOrEmpty(hdnid.Value.ToString()))  
  30. sql = "insert into payment (vessel_id,head,amount,remark) values ('" + ddlvessal.SelectedValue.ToString() + "',)";  
  31. int k = General.ExecuteQuery(sql);  
  32. }  
  33. catch (Exception ex)  
  34. {  
  35. // throw;  
  36. }  
  37. finally  
  38. { }  
  39. }  
  40. }  
  41. }  
  42. private void SetInitialRow()  
  43. {  
  44. DataTable dt = new DataTable();  
  45. DataRow dr = null;  
  46. dt.Columns.Add(new DataColumn("RowNumber"typeof(string)));  
  47. dt.Columns.Add(new DataColumn("Column1"typeof(string)));  
  48. dt.Columns.Add(new DataColumn("Column2"typeof(string)));  
  49. dt.Columns.Add(new DataColumn("Column3"typeof(string)));  
  50. dt.Columns.Add(new DataColumn("Action"typeof(string)));  
  51. dr = dt.NewRow();  
  52. dr["RowNumber"] = 1;  
  53. dr["Column1"] = string.Empty;  
  54. dr["Column2"] = string.Empty;  
  55. dr["Column3"] = string.Empty;  
  56. dt.Rows.Add(dr);  
  57. //dr = dt.NewRow();  
  58. //Store the DataTable in ViewState  
  59. ViewState["CurrentTable"] = dt;  
  60. Gridview1.DataSource = dt;  
  61. Gridview1.DataBind();  
  62. }  
  63. private void AddNewRowToGrid()  
  64. {  
  65. int rowIndex = 0;  
  66. if (ViewState["CurrentTable"] != null)  
  67. {  
  68. DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];  
  69. DataRow drCurrentRow = null;  
  70. if (dtCurrentTable.Rows.Count > 0)  
  71. {  
  72. for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)  
  73. {  
  74. //extract the TextBox values  
  75. DropDownList box1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("ddlhead");  
  76. TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txtamount");  
  77. TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("txtremark");  
  78. drCurrentRow = dtCurrentTable.NewRow();  
  79. drCurrentRow["RowNumber"] = i + 1;  
  80. dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;  
  81. dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;  
  82. dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;  
  83. rowIndex++;  
  84. }  
  85. dtCurrentTable.Rows.Add(drCurrentRow);  
  86. ViewState["CurrentTable"] = dtCurrentTable;  
  87. Gridview1.DataSource = dtCurrentTable;  
  88. Gridview1.DataBind();  
  89. }  
  90. }  
  91. else  
  92. {  
  93. Response.Write("ViewState is null");  
  94. }  
  95. //Set Previous Data on Postbacks  
  96. SetPreviousData();  
  97. }  
  98. protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)  
  99. {  
  100. }  
  101. protected void btnsave_Click(object sender, EventArgs e)  
  102. {  
  103. string sql = string.Empty;  
  104. try  
  105. {  
  106. if (string.IsNullOrEmpty(hdnid.Value.ToString()))  
  107. sql = "insert into payment (vessel_id,head,amount,remark) values ('" + ddlvessal.SelectedValue.ToString() + "',)";  
  108. int k = General.ExecuteQuery(sql);  
  109. }  
  110. catch (Exception ex)  
  111. {  
  112. // throw;  
  113. }  
  114. finally  
  115. { }  
  116. }  
  117. }  
  118. }  
Design
  1. <div class="body">  
  2. <asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false" OnSelectedIndexChanged="Gridview1_SelectedIndexChanged" style="border-collapse:collapse;" class="table table-stripped table-bordered">  
  3. <Columns>  
  4. <asp:BoundField DataField="RowNumber" HeaderText="SL No" />  
  5. <asp:TemplateField HeaderText=" Head">  
  6. <ItemTemplate>  
  7. <asp:DropDownList ID="ddlhead" class="form-control show-tick" data-live-search="true" runat="server" DataSourceId="MyDataSource" DataTextField="name" DataValueField="name"></asp:DropDownList>  
  8. <asp:HiddenField ID="hdnid" runat="server" />  
  9. </ItemTemplate>  
  10. </asp:TemplateField>  
  11. <asp:TemplateField HeaderText="Amount">  
  12. <ItemTemplate>  
  13. <asp:TextBox ID="txtamount" runat="server"></asp:TextBox>  
  14. </ItemTemplate>  
  15. </asp:TemplateField>  
  16. <asp:TemplateField HeaderText="Remark">  
  17. <ItemTemplate>  
  18. <asp:TextBox ID="txtremark" runat="server"></asp:TextBox>  
  19. </ItemTemplate>  
  20. </asp:TemplateField>  
  21. <asp:TemplateField HeaderText="Action">  
  22. <ItemTemplate>  
  23. <div class="btn-group">  
  24. <asp:LinkButton ID="lnkaddnamedelete" data-toggle="tooltip" CommandArgument="Delete"  
  25. OnClientClick="javascript:return confirm('Are you sure you want to delete this ?');"  
  26. CausesValidation="false" data-placement="bottom" title="Delete"  
  27. CssClass="fa fa-close" runat="server" onclick="lnkaddnamedelete_Click"></asp:LinkButton>  
  28. </div>  
  29. </ItemTemplate>  
  30. <FooterStyle HorizontalAlign="Right" />  
  31. <FooterTemplate>  
  32. <asp:Button ID="ButtonAdd" class="btn btn-success" CausesValidation="false" OnClick="ButtonAdd_Click" runat="server" Text="Add New" ></asp:Button>  
  33. </FooterTemplate>  
  34. </asp:TemplateField>  
  35. </Columns>  
  36. </asp:gridview>  
Please how to save the all the data in the database after click on save
  1. string sql = string.Empty;  
  2. try  
  3. {  
  4. if (string.IsNullOrEmpty(hdnid.Value.ToString()))  
  5. sql = "insert into payment (vessel_id,head,amount,remark) values ('" + ddlvessal.SelectedValue.ToString() + "',)";  
  6. int k = General.ExecuteQuery(sql);  
  7. }  
  8. catch (Exception ex)  
  9. {  
  10. // throw;  
  11. }  
  12. finally  
  13. { }  

Answers (3)