Finding and selecting a record in a gridview with paging and sortingenabled (using LINQ)

Discussion in 'ASP .Net' started by Mike N., Jul 31, 2008.

  1. Mike N.

    Mike N. Guest

    Greetings:

    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"
    ContextTypeName="TheBestMedicDataContext"
    Select="new (PlayerID, Name, Team, HoursPlayed, XP, XPHoursPlayed,
    Player)"
    TableName="Roles"
    Where="Team == @Team &amp;&amp; HoursPlayed &gt; @HoursPlayed
    &amp;&amp; Name != @Name"
    OrderBy="XPHoursPlayed desc">
    <WhereParameters>
     
    Mike N., Jul 31, 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:
    655
    Mike N.
    Jul 31, 2008
  2. George

    Linq or not Linq

    George, Nov 4, 2008, in forum: ASP .Net
    Replies:
    4
    Views:
    370
    Mike Gleason jr Couturier
    Nov 5, 2008
  3. Mike N.
    Replies:
    0
    Views:
    243
    Mike N.
    Aug 2, 2008
  4. Ryan Liu
    Replies:
    0
    Views:
    134
    Ryan Liu
    Dec 12, 2008
  5. Chuck

    Linq data source selecting and sort

    Chuck, Feb 11, 2010, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    1,476
    Chuck
    Feb 26, 2010
Loading...

Share This Page