What's problem on linq databinding -
<dx:aspxgridview id="aspxgridview1" runat="server" autogeneratecolumns="false" keyfieldname="categoryid"> <settingsediting mode="inline" /> <columns> <dx:gridviewcommandcolumn visibleindex="0"> <editbutton visible="true"></editbutton> <newbutton visible="true"></newbutton> <deletebutton visible="true"></deletebutton> </dx:gridviewcommandcolumn> <dx:gridviewdatatextcolumn caption="categoryid" fieldname="categoryid" visibleindex="1"> </dx:gridviewdatatextcolumn> <dx:gridviewdatatextcolumn caption="categoryname" fieldname="categoryname" visibleindex="2"> </dx:gridviewdatatextcolumn> <dx:gridviewdatatextcolumn caption="description" fieldname="description" visibleindex="3"> </dx:gridviewdatatextcolumn> </columns> </dx:aspxgridview>
c# syntax:
northwinddatacontext db = new northwinddatacontext(); var lresult = (db.categories .select(p => new { p.categoryid, p.categoryname, p.description})); aspxgridview1.datasource = lresult; aspxgridview1.databind();
if run code, gridview filled northwind categories table. if click on command button of grid on left side, insert/update field, have not access give input. gone read mode.
if replace above c# syntax below
northwinddatacontext db = new northwinddatacontext(); var lresult = (db.categories); aspxgridview1.datasource = lresult; aspxgridview1.databind();
then works fine. can work command button out facing problem.
i want know problem is, why first syntax not work. maybe anonymous types class types consist of 1 or more public read-only properties. when need join more 1 table , need select several fields not do. hope not linq fail or don't think possible. hope there must technique or else bind control anonymous type. plz show syntax .
the problem result set collection of anonymous type supposed , grid doesn't know how treat it. have use rowinserting , rowupdating events of grid. here example of how use devexpress grid nhibernate:
protected void gridagentgroups_rowinserting(object sender, devexpress.web.data.aspxdatainsertingeventargs e) { aspxgridview currentgrid = sender aspxgridview; var currentagentgroup = new agentgroup(); if (e.newvalues.contains("name")) { var newvalue = (string)e.newvalues["name"]; currentagentgroup.name = newvalue; } if (e.newvalues.contains("physicaladdress")) { var newvalue = (string)e.newvalues["physicaladdress"]; currentagentgroup.physicaladdress = newvalue; } agentgroupsdataaccess.saveagentgroup(currentagentgroup); e.cancel = true; currentgrid.canceledit(); currentgrid.databind(); } protected void gridagentgroups_rowupdating(object sender, devexpress.web.data.aspxdataupdatingeventargs e) { aspxgridview currentgrid = sender aspxgridview; int currentagentgroupid = (int)((agentgroup)currentgrid.getrow(currentgrid.editingrowvisibleindex)).id; var currentagentgroup = agentgroups.where(ag => ag.id == currentagentgroupid).firstordefault(); if (e.newvalues.contains("name")) { var newvalue = (string)e.newvalues["name"]; currentagentgroup.name = newvalue; } if (e.newvalues.contains("physicaladdress")) { var newvalue = (string)e.newvalues["physicaladdress"]; currentagentgroup.physicaladdress = newvalue; } agentgroupsdataaccess.saveagentgroup(currentagentgroup); e.cancel = true; currentgrid.canceledit(); currentgrid.databind(); }
i hope help.
Comments
Post a Comment