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. Windows Forms
  4. DataGrid Sorting Problem

DataGrid Sorting Problem

Scheduled Pinned Locked Moved Windows Forms
designalgorithmshelp
2 Posts 2 Posters 9 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.
  • S Offline
    S Offline
    sonia_basangar
    wrote on last edited by
    #1

    Hi, In My Datagrid i have shoen the values from tables like passport no,passenger name,travel date,tour_code. But when i click on the column Heading it is convered to Passport_no(ASc) & againi click to Passport_no(Desc) that is it just changing the heading but not actually ascending the values or descending the values of a column. I have set all sort expression for each column and in onsortcommand i write < #region DataGridSortCommand Event public void doSortDataGridItem(object sender , System.Web.UI.WebControls.DataGridSortCommandEventArgs e) { SortingDataGrid.sortDataGridByValue(dgHistory,e); }//End of this event. #endregion > This is the method i used for the sorting < public string sortDataGridByValue(DataGrid datagrid, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) { oColumnCollection=datagrid.Columns; foreach (DataGridColumn oCol in oColumnCollection) { if(e.SortExpression.CompareTo(oCol.SortExpression)== 0) { oCol.HeaderText=oCol.HeaderText.Replace(" (ASC)","").Replace(" (DESC)",""); if(e.SortExpression.IndexOf(" ASC")>1) { oCol.SortExpression = e.SortExpression.Replace(" ASC", " DESC"); oCol.HeaderText = oCol.HeaderText.Replace(" (ASC)",""); oCol.HeaderText = oCol.HeaderText + " (DESC)"; return oCol.SortExpression; }//End of inner If Block. else if (e.SortExpression.IndexOf(" DESC") > 1 ) { oCol.SortExpression = e.SortExpression.Replace(" DESC", ""); return e.SortExpression; } else { oCol.SortExpression = e.SortExpression + " ASC"; oCol.HeaderText = oCol.HeaderText + " (ASC)"; return oCol.SortExpression; } }//End of if block. }//End of For loop. return ""; }//End of method >

    K 1 Reply Last reply
    0
    • S sonia_basangar

      Hi, In My Datagrid i have shoen the values from tables like passport no,passenger name,travel date,tour_code. But when i click on the column Heading it is convered to Passport_no(ASc) & againi click to Passport_no(Desc) that is it just changing the heading but not actually ascending the values or descending the values of a column. I have set all sort expression for each column and in onsortcommand i write < #region DataGridSortCommand Event public void doSortDataGridItem(object sender , System.Web.UI.WebControls.DataGridSortCommandEventArgs e) { SortingDataGrid.sortDataGridByValue(dgHistory,e); }//End of this event. #endregion > This is the method i used for the sorting < public string sortDataGridByValue(DataGrid datagrid, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) { oColumnCollection=datagrid.Columns; foreach (DataGridColumn oCol in oColumnCollection) { if(e.SortExpression.CompareTo(oCol.SortExpression)== 0) { oCol.HeaderText=oCol.HeaderText.Replace(" (ASC)","").Replace(" (DESC)",""); if(e.SortExpression.IndexOf(" ASC")>1) { oCol.SortExpression = e.SortExpression.Replace(" ASC", " DESC"); oCol.HeaderText = oCol.HeaderText.Replace(" (ASC)",""); oCol.HeaderText = oCol.HeaderText + " (DESC)"; return oCol.SortExpression; }//End of inner If Block. else if (e.SortExpression.IndexOf(" DESC") > 1 ) { oCol.SortExpression = e.SortExpression.Replace(" DESC", ""); return e.SortExpression; } else { oCol.SortExpression = e.SortExpression + " ASC"; oCol.HeaderText = oCol.HeaderText + " (ASC)"; return oCol.SortExpression; } }//End of if block. }//End of For loop. return ""; }//End of method >

      K Offline
      K Offline
      kubben
      wrote on last edited by
      #2

      I think the best way to sort something is usually in a DataView. If you current datasource is a datatable then you need to do something like:

      DataView dv = dataSet.Tables[0].DefaultView;
      //This assumes you only have one table in your dataset

      dv.Sort = "columnName ASC"; //or you can do "columnName DESC";

      datagrid.DataSource = dv;
      datagrid.Databind();

      Hope that helps. Ben

      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