Geeks With Blogs
Beluga Consultancy

I've gathered some data into an object and then done a Linq to that object using code a bit like this:

    //Linq query
     var q = from c in objFlist.Items
             orderby c.Surname
             select c;

     List<clsFileInfo> listOfItems = q.ToList<clsFileInfo>();


      GridView2.DataSource = q;
      GridView2.DataBind();

 

That works fine as long as I let the Gridview organise itself from the data automatically.

To make it more polished I wanted to lay out the columns in the Gridview and add template columns etc. To do this I configured the grid (from it's Gridview Tasks box) to have a Linq to objects datasource with my object as the datasource. This then allows me to add and edit columns and organise it as I want. So far so good. But when I try and run it I get this error:

Both DataSource and DataSourceID are defined on 'GridView2'.  Remove one definition.

 

I've fixed this by inserting the following lines of code in but it seems there should be a better way.

      GridView2.DataSource = null;
      GridView2.DataSourceID = null;   

      GridView2.DataSource = q;
      GridView2.DataBind();

Posted on Saturday, January 23, 2010 4:26 AM | Back to top


Comments on this post: LinQ to Objects and Gridview in ASP.Net

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © BelugaNeil | Powered by: GeeksWithBlogs.net