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. gridview date sorting in asp.net?????????????????

gridview date sorting in asp.net?????????????????

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdatabasedesignalgorithms
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.
  • S Offline
    S Offline
    srinivassam
    wrote on last edited by
    #1

    Hi all, I have one doubt, i am fetching data from database and i am displaying data in gridview.(select empid,empno,covert(varchar,dateofbith,101),convert(varchar,joiningdate,101) from emptable) (values are:1,12,'12/31/1986','12/31/2008') i am not using any boundfields in gridview. i am using this code to sort the gridview columns,everything is working fine but dateofbirth,joiningdate fields sorting is not working . i am using below code to sort the gridview columns: If Not (Page.IsPostBack) Then ViewState("Sort") = "asc" end if and Protected Sub grdCommon_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grdCommon.Sorting Try grdCommon.DataSource = Session("grdCommon") grdCommon.DataBind() Dim dataTable As DataTable = CType(grdCommon.DataSource, DataTable) ' DirectCast(ViewState("grdCommon.DataSource"), DataTable) Dim dataView As DataView = New DataView(dataTable) If Not (dataView Is Nothing) Then If ViewState("Sort").ToString() = " asc" Then ViewState("Sort") = " desc" Else ViewState("Sort") = " asc" End If dataView.Sort = e.SortExpression + ViewState("Sort").ToString() grdCommon.DataSource = dataView ViewState("grdCommon.DataSource") = dataView.Table grdCommon.DataBind() End If end sub Regards Rama

    samrama

    S 1 Reply Last reply
    0
    • S srinivassam

      Hi all, I have one doubt, i am fetching data from database and i am displaying data in gridview.(select empid,empno,covert(varchar,dateofbith,101),convert(varchar,joiningdate,101) from emptable) (values are:1,12,'12/31/1986','12/31/2008') i am not using any boundfields in gridview. i am using this code to sort the gridview columns,everything is working fine but dateofbirth,joiningdate fields sorting is not working . i am using below code to sort the gridview columns: If Not (Page.IsPostBack) Then ViewState("Sort") = "asc" end if and Protected Sub grdCommon_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grdCommon.Sorting Try grdCommon.DataSource = Session("grdCommon") grdCommon.DataBind() Dim dataTable As DataTable = CType(grdCommon.DataSource, DataTable) ' DirectCast(ViewState("grdCommon.DataSource"), DataTable) Dim dataView As DataView = New DataView(dataTable) If Not (dataView Is Nothing) Then If ViewState("Sort").ToString() = " asc" Then ViewState("Sort") = " desc" Else ViewState("Sort") = " asc" End If dataView.Sort = e.SortExpression + ViewState("Sort").ToString() grdCommon.DataSource = dataView ViewState("grdCommon.DataSource") = dataView.Table grdCommon.DataBind() End If end sub Regards Rama

      samrama

      S Offline
      S Offline
      S Dhanasekaran
      wrote on last edited by
      #2

      hi, Here i add a sample code for sorting 1) In Gridview Properties add this AllowSorting="true" OnSorting="gridView1_OnSorting" 2) In gridView use TemplateField Column <asp:TemplateField HeaderText="joiningdate" HeaderStyle-HorizontalAlign="Left" SortExpression="joiningdate" ItemStyle-Width="30%"> <ItemTemplate > <span id="joiningdate" runat="server" style="color:Black;" ><%#Eval("joiningdate") %></span> </ItemTemplate> </asp:TemplateField> 3) create a Event gridView1_Onsorting protected void gridView1_OnSorting(object sender, GridViewSortEventArgs e) { string sortExpression = e.SortExpression; ViewState["sortExpression"] = e.SortExpression; if (GridViewSortDirection == SortDirection.Ascending) { GridViewSortDirection = SortDirection.Descending; SortGridView(sortExpression, "DESC"); } else { GridViewSortDirection = SortDirection.Ascending; SortGridView(sortExpression, "ASC"); } } 4) Create a SortGridView() Method private void SortGridView(string sortExpression, string direction) { DataTable dt = null; dt = "test" // fill the Datatable rows using sqlcommand or Dataset DataView dv = new DataView(dt); dv.Sort = sortExpression + " " + direction; gridView1.DataSource = dv; gridView1.DataBind(); } 5) Create a enum SortDirection public SortDirection GridViewSortDirection { get { if (ViewState["sortDirection"] == null) ViewState["sortDirection"] = SortDirection.Ascending; return (SortDirection)ViewState["sortDirection"]; } set { ViewState["sortDirection"] = value; } }

      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