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

datagrid sorting

Scheduled Pinned Locked Moved ASP.NET
algorithms
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.
  • V Offline
    V Offline
    vsk842005
    wrote on last edited by
    #1

    Hi,can anyone suggest me the simplest way of sorting a datagrid

    K R 2 Replies Last reply
    0
    • V vsk842005

      Hi,can anyone suggest me the simplest way of sorting a datagrid

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

      You have to use a DataView and use the Sort property on the dataview. You can set the AllowSorting on the datagrid to true and have it set the Sort on the DataView. Here's a link to Microsoft help: http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.allowsorting.aspx[^] Hope that helps. Ben

      1 Reply Last reply
      0
      • V vsk842005

        Hi,can anyone suggest me the simplest way of sorting a datagrid

        R Offline
        R Offline
        Rahithi
        wrote on last edited by
        #3

        Hi, we can sort the data grid on column. i saw this few days back in one article. you have tp set the AllowSorting property of the DataGrid to True. This property changes all of the column header text to hyperlinks. A click on the header text hyperlink causes a post-back and a call to the OnSortCommand event handler. In datagrid control creation u have to set these properties: AllowSorting as "True" and OnsortCommand as "SortCommand_OnClick" In the OnSortCommand event handler you simply need to specify how the data should be sorted, recreate the data source and bind the data to the DataGrid. Currently creating the data source and the data binding are done in the BindData() method, so really all you need to do is specify how the data should be sorted before calling the BindData() method. In other words, redefine the SQL statement. Since the SQL statement is a page-level variable you have access to it in the OnSortCommand event handler. u can write the OnSortCommand event handler. \\ Sub SortCommand_OnClick(Source As Object, E As DataGridSortCommandEventArgs) _sqlStmt = _sqlStmt & " ORDER BY " & E.SortExpression BindData() End Sub \\ hope this will give you some idea. Thanks, Rahithi Sharma

        If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

        S 1 Reply Last reply
        0
        • R Rahithi

          Hi, we can sort the data grid on column. i saw this few days back in one article. you have tp set the AllowSorting property of the DataGrid to True. This property changes all of the column header text to hyperlinks. A click on the header text hyperlink causes a post-back and a call to the OnSortCommand event handler. In datagrid control creation u have to set these properties: AllowSorting as "True" and OnsortCommand as "SortCommand_OnClick" In the OnSortCommand event handler you simply need to specify how the data should be sorted, recreate the data source and bind the data to the DataGrid. Currently creating the data source and the data binding are done in the BindData() method, so really all you need to do is specify how the data should be sorted before calling the BindData() method. In other words, redefine the SQL statement. Since the SQL statement is a page-level variable you have access to it in the OnSortCommand event handler. u can write the OnSortCommand event handler. \\ Sub SortCommand_OnClick(Source As Object, E As DataGridSortCommandEventArgs) _sqlStmt = _sqlStmt & " ORDER BY " & E.SortExpression BindData() End Sub \\ hope this will give you some idea. Thanks, Rahithi Sharma

          If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

          S Offline
          S Offline
          siddisagar
          wrote on last edited by
          #4

          1.Hi pls place u r connection string in place of my con string. 2. 3.paste this code in codebehind.. imports system.data.sqlclient Dim conObj As New SqlConnection Dim adpObj As New SqlDataAdapter Dim dsObj As New DataSet Dim sort As String Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then bindGrid() If sort = "" Then sort = "name" End If End If End Sub Sub bindGrid() Try conObj = New SqlConnection("Data Source=B4B-2F-323-CK26;user id=sa;password=sa;Database=TEST") conObj.Open() adpObj = New SqlDataAdapter("SELECT * FROM Login", conObj) adpObj.Fill(dsObj, "Login") Dim dt As DataTable = dsObj.Tables("Login") Dim dv As DataView = New DataView(dt) dv.Sort = sort DataGrid1.DataSource = dv DataGrid1.DataBind() Catch ex As Exception Response.Write(ex.ToString()) End Try End Sub Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand sort = e.SortExpression bindGrid() End Sub End Class :laugh:

          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