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. Web Development
  3. ASP.NET
  4. Sorting Datagrid

Sorting Datagrid

Scheduled Pinned Locked Moved ASP.NET
questionalgorithms
4 Posts 4 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.
  • H Offline
    H Offline
    happyheartcs
    wrote on last edited by
    #1

    Hi All I have a datagrid & I want it to be sorted when the user clicks on the column header..How can I do that?? Thanks a lot in advance :) Happy

    V P B 3 Replies Last reply
    0
    • H happyheartcs

      Hi All I have a datagrid & I want it to be sorted when the user clicks on the column header..How can I do that?? Thanks a lot in advance :) Happy

      V Offline
      V Offline
      varuni65
      wrote on last edited by
      #2

      it's very simple use this function in your class file or at code behind public void BindGrid(DataTable dt, DataGrid dg, string sortexpression, string order) { try { if (dt.Rows.Count > 0) { DataView dv; dv = dt.DefaultView; if (sortexpression != "") { if (order == "desc") dv.Sort = sortexpression + " DESC"; else dv.Sort = sortexpression; } dg.DataSource = dv; dg.DataBind(); } } catch (Exception ex) { clsLog.WriteLog(ex.Message); throw ex; } } where, argument1=datatable object where you hv stored the data which you would like to display 2.argument2=your datagrid id 3.argument3=variable(here u cn take sortexpression which you will declare at the beginning like this private string sortexpression = "";) 4.argument4= Convert.ToString(ViewState["order"]) now on click of column_header event write the code LinkButton lnkbtn; lnkbtn = (LinkButton)sender; if (Convert.ToString(ViewState["order"]) == "Asc") { ViewState["order"] = "desc"; } else { ViewState["order"] = "Asc"; } if (lnkbtn.Text == "1st column headername") { sortexpression = "1st column headername"; } if (lnkbtn.Text == "2ndcolumn headername") { sortexpression = "2ndcolumn headername"; } fillgrid(); (i think u know what to write in this function here just bind ur grid and one more thing in this function call the sort function with required paramts mentioned above) deepti

      1 Reply Last reply
      0
      • H happyheartcs

        Hi All I have a datagrid & I want it to be sorted when the user clicks on the column header..How can I do that?? Thanks a lot in advance :) Happy

        P Offline
        P Offline
        postmaster programmingknowledge com
        wrote on last edited by
        #3

        There is an example at : http://www.programmingknowledge.com/dataGridSorting.aspx[^]

        1 Reply Last reply
        0
        • H happyheartcs

          Hi All I have a datagrid & I want it to be sorted when the user clicks on the column header..How can I do that?? Thanks a lot in advance :) Happy

          B Offline
          B Offline
          Britney S Morales
          wrote on last edited by
          #4

          Change the allowSorting datagrid property to true go to Columns property, select the field what you want to sort, write onto the SortExpression the same column datafield. On datagrid sorting function protected void Grid_Sorting(object sender, GridViewSortEventArgs e) { string sql = "SELECT * FROM table WHERE conditions ORDER BY " + **e.SortExpression**; SqlDataAdapter data_Adapter = new SqlDataAdapter(sql, conexxxion); DataSet dataSet = new DataSet(); data_Adapter.Fill(dataSet); Grid.DataSource = dataSet.Tables[0].DefaultView; Grid.DataBind(); } you can do it with all datagrid columns :rose:

          keep Learning and you never will be out of date...

          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