Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Sorting of Gridview using LinqDataSource at runtime

Sorting of Gridview using LinqDataSource at runtime

Scheduled Pinned Locked Moved LINQ
questionalgorithmshelptutorial
3 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    K V Sekhar
    wrote on last edited by
    #1

    Hi all, I have dropdownlist, in that items('All','Confirmed','NotConfirmed') . I have 3 LinqDataSource Controls for each of dropdownlist items. Basing on selected item in dropdownlist, i am assigning LinqDataSource to Gridview. How to perform soting at runtime using LinqDataSource ? In Gridview_Sorting(..)Event i wrote like this: { if(e.SortDirection == SortDirection.Ascending) { if(ddl_items.SelectedItem.Text == "All") { LinqDataSorce_All.OrderBy = e.SortExpression + " "+SortDirection.Descending; gview.DataSourceID = "LinqDataSorce_All"; } else if(ddl_items.SelectedItem.Text == "Confirmed") { LinqDataSorce_Confirmed.OrderBy = e.SortExpression + " "+SortDirection.Descending; gview.DataSourceID = "LinqDataSorce_Confirmed"; } else if(ddl_items.SelectedItem.Text == "NotConfirmed") { LinqDataSorce_NotConfirmed.OrderBy = e.SortExpression + " "+SortDirection.Descending; gview.DataSourceID = "LinqDataSorce_NotConfirmed"; } } //end of if else { if(ddl_items.SelectedItem.Text == "All") { LinqDataSorce_All.OrderBy = e.SortExpression + " "+SortDirection.Ascending; gview.DataSourceID = "LinqDataSorce_All"; } else if(ddl_items.SelectedItem.Text == "Confirmed") { LinqDataSorce_Confirmed.OrderBy = e.SortExpression + " "+SortDirection.Ascending; gview.DataSourceID = "LinqDataSorce_Confirmed"; } else if(ddl_items.SelectedItem.Text == "NotConfirmed") { LinqDataSorce_NotConfirmed.OrderBy = e.SortExpression + " "+SortDirection.Ascending; gview.DataSourceID = "LinqDataSorce_NotConfirmed"; } }//end of else gview.DataBind(); }//end of event The Sorting is Working fine, but the problem is when i Click on Next page to gridview the soting order not remains the same. The sorted order was changed. Plz suggest me how can i over come from this? Thanks in advance.

    H 1 Reply Last reply
    0
    • K K V Sekhar

      Hi all, I have dropdownlist, in that items('All','Confirmed','NotConfirmed') . I have 3 LinqDataSource Controls for each of dropdownlist items. Basing on selected item in dropdownlist, i am assigning LinqDataSource to Gridview. How to perform soting at runtime using LinqDataSource ? In Gridview_Sorting(..)Event i wrote like this: { if(e.SortDirection == SortDirection.Ascending) { if(ddl_items.SelectedItem.Text == "All") { LinqDataSorce_All.OrderBy = e.SortExpression + " "+SortDirection.Descending; gview.DataSourceID = "LinqDataSorce_All"; } else if(ddl_items.SelectedItem.Text == "Confirmed") { LinqDataSorce_Confirmed.OrderBy = e.SortExpression + " "+SortDirection.Descending; gview.DataSourceID = "LinqDataSorce_Confirmed"; } else if(ddl_items.SelectedItem.Text == "NotConfirmed") { LinqDataSorce_NotConfirmed.OrderBy = e.SortExpression + " "+SortDirection.Descending; gview.DataSourceID = "LinqDataSorce_NotConfirmed"; } } //end of if else { if(ddl_items.SelectedItem.Text == "All") { LinqDataSorce_All.OrderBy = e.SortExpression + " "+SortDirection.Ascending; gview.DataSourceID = "LinqDataSorce_All"; } else if(ddl_items.SelectedItem.Text == "Confirmed") { LinqDataSorce_Confirmed.OrderBy = e.SortExpression + " "+SortDirection.Ascending; gview.DataSourceID = "LinqDataSorce_Confirmed"; } else if(ddl_items.SelectedItem.Text == "NotConfirmed") { LinqDataSorce_NotConfirmed.OrderBy = e.SortExpression + " "+SortDirection.Ascending; gview.DataSourceID = "LinqDataSorce_NotConfirmed"; } }//end of else gview.DataBind(); }//end of event The Sorting is Working fine, but the problem is when i Click on Next page to gridview the soting order not remains the same. The sorted order was changed. Plz suggest me how can i over come from this? Thanks in advance.

      H Offline
      H Offline
      Howard Richards
      wrote on last edited by
      #2

      You're sorting in the wrong place, as the Gridview sorting even only fires when you *change* the sort. This is not remembered on a postback. Gridview can handle the sorting itself, provided you specify the SortExpression value. It sounds like the three LinqDataSources do different selects based on the same table, and your dropdown list specifies the filter. There is an easier and simpler way: 1) replace the three LinqDataSources with just one LinqDataSource, which does the select without a filter on the table. 2) handle the LinqDataSource_Selecting event, and apply the filter to the query:

      void LinqDataSource_Selecting(.....)
      {
      var db = new MyDataContext();
      // normally I would move this to the Table class as
      // a static method, this is for demo purposes
      switch (this.DropdownList1.SelectedValue)
      {
      case "All":
      // no filter
      e.Result = from x in db.Table
      select x;

      case "Confirmed":
      // no filter
      e.Result = from x in db.Table
      where x.Status=somevalue
      select x;

      case "Unconfirmed":
      // unconfirmed filter
      e.Result = from x in db.Table
      where x.Status=someothervalue
      select x;

      default:
      // unknown
      throw new ApplicationException("unknown filter");
      }
      }

      Note that I didn't sort here, LinqDataSource will apply the sort.

      'Howard

      K 1 Reply Last reply
      0
      • H Howard Richards

        You're sorting in the wrong place, as the Gridview sorting even only fires when you *change* the sort. This is not remembered on a postback. Gridview can handle the sorting itself, provided you specify the SortExpression value. It sounds like the three LinqDataSources do different selects based on the same table, and your dropdown list specifies the filter. There is an easier and simpler way: 1) replace the three LinqDataSources with just one LinqDataSource, which does the select without a filter on the table. 2) handle the LinqDataSource_Selecting event, and apply the filter to the query:

        void LinqDataSource_Selecting(.....)
        {
        var db = new MyDataContext();
        // normally I would move this to the Table class as
        // a static method, this is for demo purposes
        switch (this.DropdownList1.SelectedValue)
        {
        case "All":
        // no filter
        e.Result = from x in db.Table
        select x;

        case "Confirmed":
        // no filter
        e.Result = from x in db.Table
        where x.Status=somevalue
        select x;

        case "Unconfirmed":
        // unconfirmed filter
        e.Result = from x in db.Table
        where x.Status=someothervalue
        select x;

        default:
        // unknown
        throw new ApplicationException("unknown filter");
        }
        }

        Note that I didn't sort here, LinqDataSource will apply the sort.

        'Howard

        K Offline
        K Offline
        K V Sekhar
        wrote on last edited by
        #3

        Thanks Richards, i will try that and let u know the result.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups