Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.

Answered LINQ IQUeryable Binding To GridView

  • Tuesday, February 05, 2008 7:52 PM
     
     

    Hello,

     

    I'm getting a LINQ query, and binding it to a gridview control in ASP.NET (I know about the ASP.NET Forums for posting ASP.NET questions, but I figured it may be applicable here too).  It won't let me page and sort, like it will a dataset or datatable. 

     

    I figure this may be because of the IListSource interface, and some of the notations that it has.  Is that right?  Is there any mechanism in .NET that will allow an IQUeryable to be paged or sorted?

     

    Thanks.

     

All Replies

  • Wednesday, February 06, 2008 1:20 AM
     
     
    You may want to give the LinqDataSource control a try. You can find more about it MSDN Library and see an example in this blog post.
  • Wednesday, February 06, 2008 2:50 PM
     
     

    Hey,

     

    I think I forgot to mention that I want to avoid the LinqDataSource because the query is setup in the designer, but I want the query to be setup in my data access component layer, so I want to be able to page/sort a result in the UI for a query that was outside the query.

     

  • Friday, May 09, 2008 9:13 PM
     
     Answered

    You could use the LinqDataSource to query against the objects in memory (the IEnumerable<T> result of your query).  Or you could use the ObjectDataSource and provide select methods that include the sorting and paging arguments.

     

    Alternatively, you could try to handle the paging and sorting manually in your query using Skip and Take.  You can do this by handling the GridView's Sorting and OnPageIndexChanging events.  The event args contain the paging and sorting parameters.  Since you're not using a data source, you'd also need to set Cancel=true in the event args and call databind manually.

  • Friday, May 09, 2008 9:27 PM
     
     
     bmains wrote:

    Hey,

     

    I think I forgot to mention that I want to avoid the LinqDataSource because the query is setup in the designer, but I want the query to be setup in my data access component layer, so I want to be able to page/sort a result in the UI for a query that was outside the query.

     

    I think this problem is due more to the fact that GridView controls can't handle sorting and paging on their own unless you use them in conjunction with a DataSource control. You need to effectively manually code the logic that does the sorting and paging of your data.

  • Saturday, January 10, 2009 1:52 PM
     
     

    hi bmains,

    I also facing the same problem for sorting and paging when I am trying to bind IList object to gridview.

    Can you please share the solution for this problem if you have got any ?

     

    Thanks in advance.

    Abhijit K.

  • Sunday, January 11, 2009 9:24 AM
     
     

     

    You can inject the query into the linq datasource selecting event. This is detailed at http://weblogs.asp.net/scottgu/archive/2007/09/07/linq-to-sql-part-9-using-a-custom-linq-expression-with-the-lt-asp-linqdatasource-gt-control.aspx

     

    e.Result = IQueryable<table> or whatever the syntax is. I think it has to be a table or entity though. Setting up the Linq relationship should give your foreign key pretty labels using the Fields of the mapped related object (to the primary table).

     

    Injecting the query actually gives you a fully functional Grid, just add the dynamic query library for a real world paged and user controlled filter.

     

    And if your query is not a table / Entiy, make a view and add it to your dbml. Pretty sure you can do the same for in memory dataset, etc, but I am a linq newb for now.

     

    Try casting, although the toList is what makes the results in memory and executed once, so...I don't know.

  • Monday, November 02, 2009 1:10 AM
     
     
    why dont you try the LinqDataSource?
    try this and please tell me if it is helpful to you http://mohamedatiasblog.wordpress.com/
  • Saturday, May 05, 2012 2:02 AM
     
      Has Code

    For anyone having this issue this is working for me:

    protected void myGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
           
           
            myGridView.DataSource =  Session["Content"] as IQueryable;
            myGridView.PageIndex = e.NewPageIndex;
            myGridView.DataBind();
        }


    mthomas