asp.net - Add gridview nested dropdown.selectedValue to database -
i have 2 tables relevant question...
tblcelltechnology celltechnologyid, pk description cellname tblactionschedule actionid, pk actionphase actionperiod celltechnology, fk
i have bulk edit grid view text boxes displaying actionid, phase, period , dropdownlist linked directly celltechnologyid tblcelltechnology (as per image). have textbox displaying cell name based on value in tblscheduleaction.celltechnologyid (actiondescription pulled table).
how can selected value ddl , write tblscheduleaction.celltechnologyid? when hard add celltechnologyid tblscheduleactionid, cellname textbox displays correct value. need able store ddl.selectedvalue tblscheduleaction.celltechnologyid. using masterpages , sqldatasources handle inserting/updating/deleting of records.
i want celltechnologyid column in bottom gridview display list of potential celltechnologyid, directly linked column top grid view, selected value should stored in tblscheduleaction.celltechnologyid. cellname column should display respective cellname. say, picture worth thousand words... http://imgur.com/21fjozh
by nested gridviews, assume referring child gridview inside each parent gridview row. getting values of child gridview controls pretty easy.
how can selected value ddl , write tblscheduleaction.celltechnologyid?
- you use
findcontrol
find childgridview inforeach
loop on save records - you use find control find dropdownlists & save database
check working example (aspx)
<%@ page language="c#" autoeventwireup="true" codebehind="nestedgridviews.aspx.cs" inherits="webapplication1.nestedgridviews" %> <!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> <asp:gridview id="gvparent" runat="server" autogeneratecolumns="false" datakeynames="categoryid" datasourceid="sdsparentgrid" onrowdatabound="gridview1_rowdatabound"> <columns> <asp:boundfield datafield="categoryid" headertext="categoryid" insertvisible="false" readonly="true" sortexpression="categoryid" /> <asp:boundfield datafield="categoryname" headertext="categoryname" sortexpression="categoryname" /> <asp:boundfield datafield="description" headertext="description" sortexpression="description" /> <asp:templatefield> <itemtemplate> <asp:gridview id="gvchildgrid" runat="server" autogeneratecolumns="false" datakeynames="productid" datasourceid="sdschildgrid"> <columns> <asp:boundfield datafield="productid" headertext="productid" insertvisible="false" readonly="true" sortexpression="productid" /> <asp:boundfield datafield="productname" headertext="productname" sortexpression="productname" /> <asp:boundfield datafield="supplierid" headertext="supplierid" sortexpression="supplierid" /> <asp:boundfield datafield="categoryid" headertext="categoryid" sortexpression="categoryid" /> <asp:checkboxfield datafield="discontinued" headertext="discontinued" sortexpression="discontinued" /> <asp:templatefield headertext="my dropdown column"> <itemtemplate> <asp:dropdownlist id="ddltest" runat="server"> <asp:listitem>potatoes</asp:listitem> <asp:listitem>tomatoes</asp:listitem> <asp:listitem>mangoes</asp:listitem> </asp:dropdownlist> </itemtemplate> </asp:templatefield> </columns> </asp:gridview> <asp:sqldatasource id="sdschildgrid" runat="server" connectionstring="<%$ connectionstrings:northwindconnectionstring %>" selectcommand="select [productid], [productname], [supplierid], [categoryid], [discontinued] [alphabetical list of products] ([categoryid] = @categoryid)"> <selectparameters> <asp:parameter name="categoryid" type="int32" /> </selectparameters> </asp:sqldatasource> </itemtemplate> </asp:templatefield> </columns> </asp:gridview> <asp:sqldatasource id="sdsparentgrid" runat="server" connectionstring="<%$ connectionstrings:northwindconnectionstring %>" selectcommand="select [categoryid], [categoryname], [description] [categories] order [categoryname]"> </asp:sqldatasource> </div> <asp:button id="btnsave" runat="server" onclick="btnsave_click" text="save database" /> </form> </body> </html>
codebehind
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; namespace webapplication1 { public partial class nestedgridviews : system.web.ui.page { protected void page_load(object sender, eventargs e) { } protected void gridview1_rowdatabound(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.datarow) { gridview gvchild = e.row.findcontrol("gvchildgrid") gridview; sqldatasource sdstemp = e.row.findcontrol("sdschildgrid") sqldatasource; if (sdstemp!=null) { sdstemp.selectparameters[0].defaultvalue = gvparent.datakeys[e.row.rowindex]["categoryid"].tostring(); if (gvchild != null) { gvchild.databind(); } } } } protected void btnsave_click(object sender, eventargs e) { foreach (gridviewrow growparent in gvparent.rows) { // find child gridview each row gridview gvchild = growparent.findcontrol("gvchildgrid") gridview; if (gvchild != null) { // use foreach loop find control foreach (gridviewrow growchild in gvchild.rows) { dropdownlist ddltemp = growchild.findcontrol("ddltest") dropdownlist; if (ddltemp != null) { response.write(ddltemp.selectedvalue.tostring()); } } } } } } }
Comments
Post a Comment