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. Datagrid sorting

Datagrid sorting

Scheduled Pinned Locked Moved C#
algorithmstutorial
2 Posts 2 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.
  • M Offline
    M Offline
    mehmetned
    wrote on last edited by
    #1

    In my application, there is a datagrid control and there are several columns on it. You know that datagrid control applies sorting data but I want to sort as multiple columns. For example, two columns are date and username. Firstly datagrid sort by date and after sort by username. Sorting priority of date greater than username. Date UserName 12.03.07 Mehmet 12.01.06 John 24.05.08 Michael 24.05.08 Anna 12.03.07 Becham Sorting by only date Date UserName 12.01.06 John 12.03.07 Mehmet 12.03.07 Becham 24.05.08 Michael 24.05.08 Anna Sorting by firstly date and after, by UserName Date UserName 12.01.06 John 12.03.07 Becham 12.03.07 Mehmet 24.05.08 Anna 24.05.08 Michael Thanks for your helps.

    T 1 Reply Last reply
    0
    • M mehmetned

      In my application, there is a datagrid control and there are several columns on it. You know that datagrid control applies sorting data but I want to sort as multiple columns. For example, two columns are date and username. Firstly datagrid sort by date and after sort by username. Sorting priority of date greater than username. Date UserName 12.03.07 Mehmet 12.01.06 John 24.05.08 Michael 24.05.08 Anna 12.03.07 Becham Sorting by only date Date UserName 12.01.06 John 12.03.07 Mehmet 12.03.07 Becham 24.05.08 Michael 24.05.08 Anna Sorting by firstly date and after, by UserName Date UserName 12.01.06 John 12.03.07 Becham 12.03.07 Mehmet 24.05.08 Anna 24.05.08 Michael Thanks for your helps.

      T Offline
      T Offline
      thomasa
      wrote on last edited by
      #2

      I think you can sort by the following codes. // Only UserName MyDateGrid.Source = MyDataView; MyDataGrid.Sort = "UserName"; MyDataGrid.DataBind(); // First UserName than date: MyDateGrid.Source = MyDataView; MyDataGrid.Sort = "UserName, theDate"; MyDataGrid.DataBind(); // First date than UserName: MyDateGrid.Source = MyDataView; MyDataGrid.Sort = "theDate, UserName"; MyDataGrid.DataBind(); You also need a property that knows what the last sorthing method was. private string lastSortCommand = ""; if(lastSortCommand == "UserName" && currentSortCommand == "theDate") { MyDateGrid.Source = MyDataView; MyDataGrid.Sort = "theDate, UserName"; MyDataGrid.DataBind(); } else if(lastSortCommand == "theDate" && currentSortCommand == "UserName") { MyDateGrid.Source = MyDataView; MyDataGrid.Sort = "UserName, theDate"; MyDataGrid.DataBind(); } else { MyDateGrid.Source = MyDataView; MyDataGrid.Sort = currentSortCommand; MyDataGrid.DataBind(); } lastSortCommand = currentSortCommand; Haven't tryed it out, but I'll think i works.

      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