asp.net - display field in gridview from another sqldatasource -
i trying implement gridview field data table, 1 of fields in table called "locationid" foreign key locations table. don't want display number on gridview want see address field locations table , edit value using dropdownlist control.
this main table in use gridview , sqldatasource
net-items [id],[name] ,[model] ,[serialnumber] ,[company] ,[purchasedate] ,[purchaseprice] ,[monthprice] ,[commitmentprice] ,[status] ,[commitmentdate] ,[free] ,[tilldate] ,[locationid]
and secondary table
net-locations [id] ,[contactname] ,[address] ,[city]
thanks
please try below code.
<%@ 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"> <h3>items details</h3> <div> <asp:sqldatasource id="sds_items" runat="server" connectionstring="<%$ connectionstrings:local %>" providername="<%$ connectionstrings:local.providername %>" selectcommand="select * items"></asp:sqldatasource> <asp:sqldatasource id="sds_location" runat="server" selectcommand="select * location" connectionstring="<%$ connectionstrings:local %>" providername="<%$ connectionstrings:local.providername %>"></asp:sqldatasource> <asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" datasourceid="sds_items" autogenerateeditbutton="true" showfooter="true"> <columns> <asp:templatefield headertext="id" > <itemtemplate> <asp:label id="id" runat="server" text='<%# bind("id") %>'></asp:label> </itemtemplate> <edititemtemplate> <asp:textbox id="txtid" runat="server" text='<%# bind("id") %>' width="98%" maxlength="6"></asp:textbox> </edititemtemplate> <footertemplate> <asp:textbox id="txtnewid" runat="server" text='<%# bind("id") %>' width="98%" maxlength="6"></asp:textbox> </footertemplate> <itemstyle width="80px" /> </asp:templatefield> <asp:templatefield headertext="location" > <itemtemplate> <asp:label id="lbllocation" runat="server" text='<%# lookuplocation(databinder.eval(container.dataitem, "locationid")) %>'></asp:label> </itemtemplate> <edititemtemplate> <asp:dropdownlist id="ddllocation" runat="server" datasourceid="sds_location" datatextfield="id" datavaluefield="id" selectedvalue='<%#bind("locationid")%>' width="98%"> </asp:dropdownlist> </edititemtemplate> <footertemplate> <asp:dropdownlist id="ddlnewlocation" runat="server" datasourceid="sds_location" datatextfield="address" datavaluefield="id" selectedvalue='<%#bind("locationid")%>' width="95%"> </asp:dropdownlist> </footertemplate> <itemstyle width="25%" /> </asp:templatefield> </columns> </asp:gridview> </div> </form> </body> </html> using system; using system.collections.generic; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.collections; using system.data; public partial class _default : system.web.ui.page { protected void page_load(object sender, eventargs e) { } protected string lookuplocation(object idobj) { if (string.isnullorempty(idobj.tostring())) return null; // store permissionid passed input parameter string locationid = idobj.tostring(); // find corresponding name ienumerator enumos = sds_location.select(new datasourceselectarguments()).getenumerator(); while (enumos.movenext()) { datarowview row = enumos.current datarowview; if ((string)row["id"].tostring() == locationid) return string.concat(row["address"].tostring()); } return locationid; } }
Comments
Post a Comment