Answered by:
Update all records at once after editing data in Grid view

Question
-
User-729180281 posted
I want to display data in a grid view, edit data all records, and update the database in one click of a button rather than editing each record and updating it befor moving to the next, how do we do this in Visual studio
Friday, June 17, 2016 3:56 PM
Answers
-
User61956409 posted
Hi nkabirwa,
You could display fields with textbox inside GridView, then you could loop through GridView rows to find fields’ value and update records on update button click event.
<div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="chkupdate" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="ID"> <ItemTemplate> <asp:Label ID="lblid" runat="server" Text='<%#Eval("ID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Field1"> <ItemTemplate> <asp:TextBox ID="txtfield1" runat="server" Text='<%#Eval("Field1") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Field2"> <ItemTemplate> <asp:TextBox ID="txtfield2" runat="server" Text='<%#Eval("Field2") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <br /> <asp:Button ID="btnupdate" runat="server" Text="Update" OnClick="btnupdate_Click" /> </div>
protected void btnupdate_Click(object sender, EventArgs e) { for (int i = 0; i < GridView1.Rows.Count; i++) { CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("chkupdate"); //if op checked checkbox and want to update the row record if (chk.Checked) { string id = ((Label)GridView1.Rows[i].FindControl("lblid")).Text; //find other fields' value //updated records } } }
Best Regards,
Fei Han
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, June 18, 2016 2:32 AM -
User-1034726716 posted
I want to display data in a grid view, edit data all records, and update the database in one click of a buttonThe standard GridView edit functionality only allows you to edit one row at a time. If you want to implement bulk edit then you need to use TemplateFields with TextBoxes. Here's an article that may help you to get started: http://www.c-sharpcorner.com/UploadFile/8c19e8/dynamically-adding-and-deleting-rows-in-gridview-and-saving/
Saving of rows at once is also covered in that article.
You could also use AJAX and build your own HTML grid by hand or use an existing grid plugin to cater such feature, but I believed that would take you a lot of work/time for it to make it work.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 20, 2016 3:08 PM
All replies
-
User61956409 posted
Hi nkabirwa,
You could display fields with textbox inside GridView, then you could loop through GridView rows to find fields’ value and update records on update button click event.
<div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="chkupdate" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="ID"> <ItemTemplate> <asp:Label ID="lblid" runat="server" Text='<%#Eval("ID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Field1"> <ItemTemplate> <asp:TextBox ID="txtfield1" runat="server" Text='<%#Eval("Field1") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Field2"> <ItemTemplate> <asp:TextBox ID="txtfield2" runat="server" Text='<%#Eval("Field2") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <br /> <asp:Button ID="btnupdate" runat="server" Text="Update" OnClick="btnupdate_Click" /> </div>
protected void btnupdate_Click(object sender, EventArgs e) { for (int i = 0; i < GridView1.Rows.Count; i++) { CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("chkupdate"); //if op checked checkbox and want to update the row record if (chk.Checked) { string id = ((Label)GridView1.Rows[i].FindControl("lblid")).Text; //find other fields' value //updated records } } }
Best Regards,
Fei Han
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, June 18, 2016 2:32 AM -
User-1034726716 posted
I want to display data in a grid view, edit data all records, and update the database in one click of a buttonThe standard GridView edit functionality only allows you to edit one row at a time. If you want to implement bulk edit then you need to use TemplateFields with TextBoxes. Here's an article that may help you to get started: http://www.c-sharpcorner.com/UploadFile/8c19e8/dynamically-adding-and-deleting-rows-in-gridview-and-saving/
Saving of rows at once is also covered in that article.
You could also use AJAX and build your own HTML grid by hand or use an existing grid plugin to cater such feature, but I believed that would take you a lot of work/time for it to make it work.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 20, 2016 3:08 PM