Question

I have a pretty complicated Linq query that I can't seem to get into a LinqDataSsource for use in a GridView:

IEnumerable<ticket> tikPart = (
    from p in db.comments where 
        p.submitter == me.id && 
        p.ticket.closed == DateTime.Parse("1/1/2001") && 
        p.ticket.originating_group != me.sub_unit 
    select p.ticket
    ).Distinct();

How can I get this into a GridView? Thank you!

Was it helpful?

Solution

You can setup your Gridview with no Datasource. Setup the gridview columns, and in codebehind bind that result to the grid view.

OTHER TIPS

gridview.DataSource = tikPart.ToList();
gridview.DataBind();

@leppie - There is no need to call a ToList() on the IQueryable when attaching it as a data source.

Provided your DataContext has not been disposed of prior to the DataBind method being called ToList is a redundant call.

By default a DataContext uses lazy-loading, so that the data is only fetched from the database when the IQueryable is Enumerated. ToList() performs an Enumeration and does the call, so does DataBind().

So you can do something like this:

using(MyDataContext ctx = new MyDataContext(){
  this.MyGridView.DataSource = from something in ctx.Somethings where something.SomeProperty == someValue select something;
  this.MyGridView.DataBind();
}

Depending on how your disposing your DataContext determins what to bind to a data source.

You can then either use auto generated columns on the GridView, so that every property in your returned object is turned into a column, or you can write the columns with the designer and set up the binding rules there.

You can bind IQueryable<> type to GridView using LinqDataSource control. http://johnsobrepena.blogspot.com/2010/01/data-bind-coolgridview-to-iqueryable.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top