Sunday, 15 September 2013

c# - How to connect a Button to the Checkboxes in a GridView, and delete the selected rows from the GridView(ticked via CheckBoxes integrated in GridView)? -



c# - How to connect a Button to the Checkboxes in a GridView, and delete the selected rows from the GridView(ticked via CheckBoxes integrated in GridView)? -

i have created gridview containing info extracted textbox. have "delete link" on gridview delete row gridview if required.

now want create changes it. instead of "delete link" on gridview, want checkboxes on each row of gridview. outside gridview, there should button. on clicking button, rows selected via checkbox on gridview should deleted.

what changes has made below code implement functionality? please specify changes clearly.

default.aspx

<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <br /> employee id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:textbox id="textbox1" runat="server"></asp:textbox> <br /> <br /> employee name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:textbox id="textbox2" runat="server"></asp:textbox> <br /> <br /> salary&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:textbox id="textbox3" runat="server"></asp:textbox> <br /> <br /> <br /> <asp:button id="button1" runat="server" text="add grid" onclick="button1_click" /> <br /> <br /> <br /> <br /> <br /> <asp:button id="button2" runat="server" text="export info database" onclick="button2_click" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /> <br /> <asp:label id="label1" runat="server"></asp:label> <br /> <br /> <asp:gridview id="gridview1" runat="server" datakeynames="empid" autogeneratecolumns="false" onrowediting="gridview1_rowediting" onrowcancelingedit="gridview1_rowcancelingedit" onrowdeleting="gridview1_rowdeleting" onpageindexchanging="gridview1_pageindexchanging" pagesize="5" allowpaging="true" onrowupdating="gridview1_rowupdating" width="800"> <columns> <asp:templatefield headertext="employee id"> <itemtemplate> <%#eval("empid")%> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="employee name"> <itemtemplate> <%#eval("empname")%> </itemtemplate> <edititemtemplate> <asp:textbox id="txtempname" runat="server" text='<%#eval("empname") %>'></asp:textbox> </edititemtemplate> </asp:templatefield> <asp:templatefield headertext="salary"> <itemtemplate> <%#eval("empsalary")%> </itemtemplate> <edititemtemplate> <asp:textbox id="txtempsalary" runat="server" text='<%#eval("empsalary") %>'></asp:textbox> </edititemtemplate> </asp:templatefield> <asp:commandfield headertext="modify" showeditbutton="true" edittext="edit"> <controlstyle width="50" /> </asp:commandfield> <asp:templatefield headertext="delete"> <itemtemplate> <asp:linkbutton id="lnkdelete" commandname="delete" runat="server" onclientclick="return confirm('are sure want delete these records?');">delete</asp:linkbutton> </itemtemplate> </asp:templatefield> </columns> </asp:gridview> </div> <p style="width: 799px"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:button id="button3" runat="server" onclick="button3_click" text="delete checked items" width="162px" /> </p> <p> &nbsp;</p> <p> &nbsp;</p> <p> <asp:gridview id="gridview2" runat="server" backcolor="white" bordercolor="black" borderstyle="solid" borderwidth="1px" cellpadding="3" width="580px"> <pagerstyle horizontalalign="left" /> </asp:gridview> </p> </form> <p> &nbsp;</p> </body> </html>

sample.aspx.cs

using system; using system.collections; using system.collections.generic; using system.configuration; using system.web; using system.web.security; using system.web.ui; using system.web.ui.htmlcontrols; using system.web.ui.webcontrols; using system.web.ui.webcontrols.webparts; using system.linq; using system.xml.linq; using system.data; using system.data.sqlclient; public partial class _default : system.web.ui.page { sqlconnection sqlcon = new sqlconnection(configurationmanager.appsettings["constring"]); sqlcommand sqlcmd = new sqlcommand(); sqldataadapter da = new sqldataadapter(); datatable dt = new datatable(); datatable dt1 = new datatable(); datarow dr; datarow dr1; dataset ds = new dataset(); protected void page_load(object sender, eventargs e) { label1.text = ""; //lbldbmsg.text = ""; if (!page.ispostback) { dt.columns.add("empid"); dt.columns.add("empname"); dt.columns.add("empsalary"); session["reptable"] = dt; griddata(); } } protected void gridview1_rowediting(object sender, gridviewediteventargs e) { gridview1.editindex = e.neweditindex; griddata(); } void griddata() { gridview1.datasource = (datatable)session["reptable"]; gridview1.databind(); } protected void gridview1_rowcancelingedit(object sender, gridviewcancelediteventargs e) { gridview1.editindex = -1; griddata(); } protected void gridview1_rowupdating(object sender, gridviewupdateeventargs e) { gridviewrow row = gridview1.rows[e.rowindex]; string empid; empid = gridview1.datakeys[e.rowindex].value.tostring(); textbox empname = (textbox)row.findcontrol("txtempname"); textbox empsalary = (textbox)row.findcontrol("txtempsalary"); if (session["reptable"] != null) { datatable dt1 = new datatable(); dt1.clear(); dt1 = session["reptable"] datatable; (int = 0; <= dt1.rows.count - 1; i++) { datarow dr; if (dt1.rows[i][0].tostring() == empid) { dr = dt1.rows[i]; dt1.rows[i].delete(); } } session.remove("reptable"); session["reptable"] = dt1; //add updated row here dt = (datatable)session["reptable"]; dr1 = dt.newrow(); dr1["empid"] = empid; dr1["empname"] = empname.text; dr1["empsalary"] = empsalary.text; dt.rows.add(dr1); session.remove("reptable"); session["reptable"] = dt; } gridview1.editindex = -1; griddata(); } protected void gridview1_rowdeleting(object sender, gridviewdeleteeventargs e) { string empid; empid = gridview1.datakeys[e.rowindex].value.tostring(); if (session["reptable"] != null) { datatable dt1 = new datatable(); dt1.clear(); dt1 = session["reptable"] datatable; (int = 0; <= dt1.rows.count - 1; i++) { datarow dr; if (dt1.rows[i][0].tostring() == empid) { dr = dt1.rows[i]; dt1.rows[i].delete(); //dt1.rows.remove(dr); } } session.remove("reptable"); session["reptable"] = dt1; } griddata(); } protected void gridview1_pageindexchanging(object sender, gridviewpageeventargs e) { gridview1.pageindex = e.newpageindex; griddata(); } protected void button1_click(object sender, eventargs e) { dt = (datatable)session["reptable"]; dr = dt.newrow(); dr["empid"] = textbox1.text; dr["empname"] = textbox2.text; dr["empsalary"] = textbox3.text; dt.rows.add(dr); session.remove("reptable"); session["reptable"] = dt; griddata(); textbox1.text = ""; textbox2.text = ""; textbox3.text = ""; } //bulk insert info sql server database protected void button2_click(object sender, eventargs e) { dt = (datatable)session["reptable"]; //upload info database using mass re-create sqlbulkcopy sqlbulk = new sqlbulkcopy(configurationmanager.appsettings["constring"]); sqlbulk.destinationtablename = "emp"; //table name sqlbulk.writetoserver(dt); //remove info after insert dt.clear(); session["reptable"] = dt; griddata(); label1.text = "all records inserted database"; } protected void button3_click(object sender, eventargs e) { } }

try this..

aspx code:

add checkbox command in item template of gridview..

<asp:templatefield> <itemtemplate> <asp:checkbox id="chkdelete" runat="server" /> </itemtemplate> </asp:templatefield>

c# code:

protected void button3_click(object sender, eventargs e) { foreach (gridviewrow gvrow in gridview1.rows) { //finiding checkbox command in gridview particular row checkbox chkdelete = (checkbox)gvrow.findcontrol("chkdelete"); //condition check checkbox selected or not if (chkdelete.checked) { if (session["reptable"] != null) { string empid = gridview1.datakeys[gvrow.rowindex].value.tostring(); datatable dt1 = new datatable(); dt1.clear(); dt1 = session["reptable"] datatable; (int = 0; <= dt1.rows.count - 1; i++) { datarow dr; if (dt1.rows[i][0].tostring() == empid) { dr = dt1.rows[i]; dt1.rows[i].delete(); //dt1.rows.remove(dr); } } //session.remove("reptable"); session["reptable"] = dt1; } } } griddata(); }

c# asp.net button gridview datatable

No comments:

Post a Comment