Binding LinqDataSource from code-behind to Gridview

假如想象 提交于 2019-12-11 06:26:55

问题


i have a grdidview control on the .aspx page and i am trying to connect dynamically from code behind and bind the gridview but somehow it throwing me an error... what is wrong with this code? any help?

  LinqDataSource LDS_POReport = new LinqDataSource();
            LDS_POReport.ContextTypeName = "DataContextDataContext";
            LDS_POReport.Selecting += new EventHandler<LinqDataSourceSelectEventArgs>(LinqDataSourcePO_Selecting);
            this.gvReport.DataSource = "LDS_POReport";
            //this.gvReport.DataBind();

Update:

after i update the code to

 this.gvReport.DataSource = LDS_POReport;

it works fine but when i try to sort i get this error:

The GridView 'gvReport' fired event Sorting which wasn't handled.

i added this but no effect.

 LDS_POReport.AutoPage = true;
 LDS_POReport.AutoSort = true;

回答1:


I guess that your problem is here:

this.gvReport.DataSource = "LDS_POReport";

The above code line attempts to assign a string to a property that expects some sort of data source. I assume that you really intended to assign the LinqDataSource object itself:

this.gvReport.DataSource = LDS_POReport;



回答2:


First thing, the DataSource should get a reference to the object containing the data, not the name of the object containing the data. GridViews can work reflectively, but not THAT reflectively.



来源:https://stackoverflow.com/questions/3670549/binding-linqdatasource-from-code-behind-to-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!