c# - Only approve the row that is checked from asp gridview -
i have gridview looks wherein have several rows : name, contact#, company name etc. , have included check box can choose row approve or not.
<asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" cssclass="table table-hover table-striped" enableviewstate="false" onselectedindexchanged="gridview1_selectedindexchanged"> <columns> <asp:templatefield> <itemtemplate> <asp:checkbox id="chkrow" runat="server" /> </itemtemplate> </asp:templatefield> <asp:boundfield datafield="username" headertext="username" sortexpression="username" /> <asp:boundfield datafield="lastname" headertext="lastname" sortexpression="lastname" /> <asp:boundfield datafield="firstname" headertext="firstname" sortexpression="firstname" /> <asp:boundfield datafield="companyname" headertext="companyname" sortexpression="companyname" /> <asp:boundfield datafield="emailaddress" headertext="emailaddress" sortexpression="emailaddress" /> <asp:boundfield datafield="companyaddress" headertext="companyaddress" sortexpression="companyaddress" /> <asp:boundfield datafield="incomerange" headertext="incomerange" sortexpression="incomerange" /> <asp:boundfield datafield="creditrequest" headertext="creditrequest" sortexpression="creditrequest" /> <asp:boundfield datafield="contactnumber" headertext="contactnumber" sortexpression="contactnumber" /> <%--<asp:templatefield> <itemtemplate>--%> <%--</itemtemplate> </asp:templatefield>--%> <asp:checkboxfield /> <asp:checkboxfield /> </columns> </asp:gridview> <asp:button id="button1" runat="server" text="approve" onclick="button1_click" />
what want happen when click on approve button, row checked perform code behind.
here code behind:
protected void button1_click(object sender, eventargs e) { using (sqlconnection scn = new sqlconnection("data source = 'paulo'; initial catalog=shoppingcartdb;integrated security =true")) { scn.open(); sqlcommand cmd = new sqlcommand(@"update userdata set creditrequest = cast(replace(c.creditrequest, ',', '') int) creditrequests c inner join userdata u on c.username=u.username c.username=@username", scn); cmd.parameters.add("@username", sqldbtype.nvarchar).value = dsession["new"]; cmd.executenonquery(); } }
update ( tried this, has no errors approve function not performing)
protected void approve(string username) { using (sqlconnection scn = new sqlconnection("data source = 'paulo'; initial catalog=shoppingcartdb;integrated security =true")) { scn.open(); sqlcommand cmd = new sqlcommand(@"update userdata set creditrequest = cast(replace(c.creditrequest, ',', '') int) creditrequests c inner join userdata u on c.username=u.username c.username=@username", scn); cmd.parameters.add("@username", sqldbtype.nvarchar).value = session["new"]; cmd.executenonquery(); } } protected void button1_click(object sender, eventargs e) { foreach (gridviewrow grow in gridview1.rows) { //searching checkbox("chkdel") in individual row of grid checkbox chkdel = (checkbox)grow.findcontrol("chkdel"); //if checkbox checked delete record particular empid if (chkdel.checked) { string username = grow.cells[1].text; approve(username); } } //displaying data in gridview bindgrid(); }
add datakeynames="ur primary column"
in gridview html
. , in code behind loop through rows in gridview below.
//loop through rows in gridview foreach (gridviewrow grv in grdemp.rows) { //finiding checkbox control in gridview particular row checkbox chk = (checkbox)grv.findcontrol("chkdel"); if (chk.checked) { //get empid datakeynames gridview int empid = convert.toint32(grdemp.datakeys[grv.rowindex].value); cmd = new sqlcommand("update_sp", con); //put ur inline query here cmd.parameters.add("@username", sqldbtype.int).value = username; //use per ur requirments cmd.commandtype = commandtype.storedprocedure; cmd.executenonquery(); } } grdemp.editindex = -1; bindempgrid();
Comments
Post a Comment