Saturday, January 23, 2010 #

My Videography has moved to another blog

I've moved all my posts on Videography to my other blog.

http://www.belugaconsultancy.blogspot.com/

I'm keeping this for more technical programming blog posts. It's mostly for my own consumption but happy if it helps anyone else.

Posted On Saturday, January 23, 2010 6:31 AM | Feedback (0)

LinQ to Objects and Gridview in ASP.Net

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 | Feedback (0)

Copyright © BelugaNeil

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski