moving to records in a gridview with paging and sorting enabled(LINQ)

Discussion in 'ASP .Net Web Controls' started by Mike N., Aug 2, 2008.

  1. Mike N.

    Mike N. Guest

    Greetings:

    First know that I'm not a programmer by trade, I a hobbyist programmer
    working on a website for a game. background on my problem:

    I have a gridview that has paging and sorting enabled. I have it
    hooked up to a LinqDatasource which provides an initial where clause.
    This where is modified by the user using a dropdown list so the user
    can filter the list further like so:

    switch (DropDownList1.SelectedValue)
    {
    case "All":
    LinqDataSource2.Where = "HoursPlayed >
    @HoursPlayed && Team ==
    @Team && Name != @Name";
    break;
    default:
    LinqDataSource2.Where = "HoursPlayed >
    @HoursPlayed && Team ==
    @Team && Name == @RoleName";
    break;
    }

    The first column in my gridview is the number of the record in the
    current sort order. If the page length is 10 and the user is on the
    second record of page 2 the first row will have the number 12 in it,
    and so on through the pages. (I just do the numbering with a template
    control and the following as the itemtemplate: <%#
    (Container.DataItemIndex + 1) + (GridView2.PageIndex *
    GridView2.PageSize) %>

    I use this grdiview as a ranking table. When the user re-sorts the
    table it comes up with a new ranking based on the column he's sorted
    on and the filter he's provided.

    I have all of the above working fine, here is my predicament:

    I want the user to be able to input a value (UserName) and then have
    the gridview go to the proper page and select the appropriate row.

    It seems to me the easiest way to do this would be to get at the
    dataset inside the LinqDataSource and find the index of the record I
    am looking for and then set the gridview's pageindex appropriately and
    select the row on the page. I've been unable to figure out how to get
    that dataset, so I've resorted to creating a new query using the same
    parameters the LinqDatasource is using in it's where clause and order
    by. First I set the where like this:

    var rolelist = from r in dbBestMedic.Roles
    select r;

    switch (DropDownList1.SelectedValue)
    {
    case "All":
    rolelist = rolelist.Where(r =>
    r.HoursPlayed > minHours && r.Team
    == 3 && r.Name != "Classes");
    break;
    default:
    rolelist = rolelist.Where(r =>
    r.HoursPlayed > minHours && r.Name
    == DropDownList1.SelectedValue);
    break;
    }

    Then I set the orderby like this:

    switch (GridView2.SortExpression.ToString())
    {
    case "Player.UserName":
    rolelist =
    rolelist.OrderByDescending(r => r.Player.UserName);
    break;
    case "HoursPlayed":
    rolelist =
    rolelist.OrderByDescending(r => r.HoursPlayed);
    break;
    case "XP":
    rolelist =
    rolelist.OrderByDescending(r => r.XP);
    break;
    case "XPHoursPlayed":
    rolelist =
    rolelist.OrderByDescending(r => r.XPHoursPlayed);
    break;
    default:
    rolelist =
    rolelist.OrderByDescending(r => r.XPHoursPlayed);
    break;
    }

    Then I iterate through the query (rolelist) to get the index and set
    it in my gridview

    Aside from thinking there has to be an easier way (is there and easier
    way?), this fails if the field I am sorting by has values that aren't
    unique.

    So now I am trying to ad a second orderby field to ensure a unique
    value between the first sort field and the second combined. When I do
    this though it seems to me the data is being sorted differently in the
    Linqdatasource's orderby clause:

    <asp:LinqDataSource ID="LinqDataSource2" runat="server"
    ....
    OrderBy="XPHoursPlayed desc, PlayerID desc">
    ....

    and the sort expression I set for my Linq query:
    switch (GridView2.SortExpression.ToString())
    {
    ....
    case "XP":
    rolelist = rolelist.OrderByDescending(r =>
    r.XP).ThenOrderByDescending(r=>r.PlayerID;
    break;
    ....

    because this breaks my routine and the query I am iterating through is
    no longer in the same order as the Linqdatasource's data going to the
    gridview (but it seems to me they should be as the orderby clause and
    the Linq expression should be equivalent.

    Sorry for the long post, felt i needed all of this to explain my
    predicament properly.
     
    Mike N., Aug 2, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike N.
    Replies:
    0
    Views:
    502
    Mike N.
    Jul 31, 2008
  2. Mike N.
    Replies:
    0
    Views:
    643
    Mike N.
    Jul 31, 2008
  3. Replies:
    4
    Views:
    945
    Allen Chen [MSFT]
    Sep 2, 2008
  4. George

    Linq or not Linq

    George, Nov 4, 2008, in forum: ASP .Net
    Replies:
    4
    Views:
    361
    Mike Gleason jr Couturier
    Nov 5, 2008
  5. Ryan Liu
    Replies:
    0
    Views:
    129
    Ryan Liu
    Dec 12, 2008
Loading...

Share This Page