1

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;
Nick Kahn
  • 19,652
  • 91
  • 275
  • 406
  • 1
    When asking a question like this, please *always* include exception details. "It throws an error" is not very much to draw any conclusions from. – Fredrik Mörk Sep 08 '10 at 17:54

2 Answers2

3

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;
Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
  • Fred, it works but sorting/paging does not work i get this error "The GridView 'gvReport' fired event Sorting which wasn't handled" – Nick Kahn Sep 08 '10 at 17:58
  • @Nishar: when `AllowSorting` is `true` you need to set up an event handler for the `Sorting` event. It seems that no such event handler is present. – Fredrik Mörk Sep 08 '10 at 18:03
0

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.

KeithS
  • 70,210
  • 21
  • 112
  • 164