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. C#
  4. Sorting in multiple columns in gridview using C#

Sorting in multiple columns in gridview using C#

Scheduled Pinned Locked Moved C#
csharpalgorithmshelpquestion
7 Posts 3 Posters 0 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.
  • D Offline
    D Offline
    Dhyanga
    wrote on last edited by
    #1

    Hi, I am using sorting feature in multiple columns in a gridview. My gridview sorting is like this:

    protected void gView_Sorting(object sender, GridViewSortEventArgs e)
    {
    //checking direction for sorting
    try
    {
    if (this.ParentPortlet.PortletViewState["direction"].ToString() == "")
    {
    this.ParentPortlet.PortletViewState["direction"] = " ASC";
    }
    else if (this.ParentPortlet.PortletViewState["direction"].ToString() == " ASC")
    {
    this.ParentPortlet.PortletViewState["direction"] = " DESC";
    }
    else if (this.ParentPortlet.PortletViewState["direction"].ToString() == " DESC")
    {
    this.ParentPortlet.PortletViewState["direction"] = " ASC";
    }
    }
    catch
    {
    this.ParentPortlet.PortletViewState["direction"] = " DESC";
    }
    getData(e.SortExpression.ToString(), this.ParentPortlet.PortletViewState["direction"].ToString());
    }

    I have four columns in the gridview (Name, Date, Location, Cell) and all these columns are sortable. so If i click name, it sorts in desc order and again if i click it, it will sort in asc order which is great. Similar with the other fields. But my problem is If I click the Name, it will sort in desc order. Now If i click Date, it will sort in asc order. Again If I click the Name, it will sort in desc order. Is there any way we can put these sortings independent to each column. Like, If I click Name, it should sort in desc order. Now If I click date, it should sort in desc order. Now again, If i click Name, it should sort in asc order. Any ideas?

    suchita

    N L 2 Replies Last reply
    0
    • D Dhyanga

      Hi, I am using sorting feature in multiple columns in a gridview. My gridview sorting is like this:

      protected void gView_Sorting(object sender, GridViewSortEventArgs e)
      {
      //checking direction for sorting
      try
      {
      if (this.ParentPortlet.PortletViewState["direction"].ToString() == "")
      {
      this.ParentPortlet.PortletViewState["direction"] = " ASC";
      }
      else if (this.ParentPortlet.PortletViewState["direction"].ToString() == " ASC")
      {
      this.ParentPortlet.PortletViewState["direction"] = " DESC";
      }
      else if (this.ParentPortlet.PortletViewState["direction"].ToString() == " DESC")
      {
      this.ParentPortlet.PortletViewState["direction"] = " ASC";
      }
      }
      catch
      {
      this.ParentPortlet.PortletViewState["direction"] = " DESC";
      }
      getData(e.SortExpression.ToString(), this.ParentPortlet.PortletViewState["direction"].ToString());
      }

      I have four columns in the gridview (Name, Date, Location, Cell) and all these columns are sortable. so If i click name, it sorts in desc order and again if i click it, it will sort in asc order which is great. Similar with the other fields. But my problem is If I click the Name, it will sort in desc order. Now If i click Date, it will sort in asc order. Again If I click the Name, it will sort in desc order. Is there any way we can put these sortings independent to each column. Like, If I click Name, it should sort in desc order. Now If I click date, it should sort in desc order. Now again, If i click Name, it should sort in asc order. Any ideas?

      suchita

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Using a Hashtable, or similar structure, to store the sort direction based on column is about the only way to get this done I can think of at the moment


      No comment

      D 1 Reply Last reply
      0
      • N Not Active

        Using a Hashtable, or similar structure, to store the sort direction based on column is about the only way to get this done I can think of at the moment


        No comment

        D Offline
        D Offline
        Dhyanga
        wrote on last edited by
        #3

        Can you point me to any example on this, please?

        suchita

        N 1 Reply Last reply
        0
        • D Dhyanga

          Can you point me to any example on this, please?

          suchita

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          How to work with the HashTable collection in Visual C#[^]


          No comment

          1 Reply Last reply
          0
          • D Dhyanga

            Hi, I am using sorting feature in multiple columns in a gridview. My gridview sorting is like this:

            protected void gView_Sorting(object sender, GridViewSortEventArgs e)
            {
            //checking direction for sorting
            try
            {
            if (this.ParentPortlet.PortletViewState["direction"].ToString() == "")
            {
            this.ParentPortlet.PortletViewState["direction"] = " ASC";
            }
            else if (this.ParentPortlet.PortletViewState["direction"].ToString() == " ASC")
            {
            this.ParentPortlet.PortletViewState["direction"] = " DESC";
            }
            else if (this.ParentPortlet.PortletViewState["direction"].ToString() == " DESC")
            {
            this.ParentPortlet.PortletViewState["direction"] = " ASC";
            }
            }
            catch
            {
            this.ParentPortlet.PortletViewState["direction"] = " DESC";
            }
            getData(e.SortExpression.ToString(), this.ParentPortlet.PortletViewState["direction"].ToString());
            }

            I have four columns in the gridview (Name, Date, Location, Cell) and all these columns are sortable. so If i click name, it sorts in desc order and again if i click it, it will sort in asc order which is great. Similar with the other fields. But my problem is If I click the Name, it will sort in desc order. Now If i click Date, it will sort in asc order. Again If I click the Name, it will sort in desc order. Is there any way we can put these sortings independent to each column. Like, If I click Name, it should sort in desc order. Now If I click date, it should sort in desc order. Now again, If i click Name, it should sort in asc order. Any ideas?

            suchita

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi, I don't know about GridView. However I can tell you about WinForms' DataGridView: 1. if you provide no code at all, it will sort itself as it should; each column remembers its sort direction, and will toggle it every time you hit the column header without having hit another column header. 2. It does not have a Sorting event (i.e. no pre-Sort), it does have a Sorted event (post-Sort). BTW: GridViewSortingEventArgs.SortDirection tells you how the GridView is going to be sorted. Either it is column-specific or unique for the entire GridView, I don't know (and MSDN seems not to be clear on this). If it is column-specific, you should just use it; if it is a single bool covering all columns, you need to replace it by one bool per column, a HashTable could help you do that. :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            D 1 Reply Last reply
            0
            • L Luc Pattyn

              Hi, I don't know about GridView. However I can tell you about WinForms' DataGridView: 1. if you provide no code at all, it will sort itself as it should; each column remembers its sort direction, and will toggle it every time you hit the column header without having hit another column header. 2. It does not have a Sorting event (i.e. no pre-Sort), it does have a Sorted event (post-Sort). BTW: GridViewSortingEventArgs.SortDirection tells you how the GridView is going to be sorted. Either it is column-specific or unique for the entire GridView, I don't know (and MSDN seems not to be clear on this). If it is column-specific, you should just use it; if it is a single bool covering all columns, you need to replace it by one bool per column, a HashTable could help you do that. :)

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              D Offline
              D Offline
              Dhyanga
              wrote on last edited by
              #6

              I have no idea how do we use hashtable for each column event for gridview.. :(

              suchita

              L 1 Reply Last reply
              0
              • D Dhyanga

                I have no idea how do we use hashtable for each column event for gridview.. :(

                suchita

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                you want to hold one boolean flag for every column, but have them structured, not just a bunch of loose flags. There are several ways to do that. One way is by holding an array of four bools, one for each column, and use the column number as the array index. A more elastic approach would rely on a HashTable or better yet a Dictionary<string,bool> where the key could be the column header text (they have to be unique!), and the value is the bool itself. That way you can find the bool for a column by feeding the column header text to the HashTable/Dictionary. BTW: you could use the Column itself as the key, however that may or may not be any easier. :) PS: if you're new to HashTable/Dictionary you should read up on those classes.

                Luc Pattyn [My Articles] Nil Volentibus Arduum

                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