Welcome to Strong Coders Community Sign in | Join | Help

Blog ( Of Ryan Olshan)

News

  • About Ryan Olshan

    Join StrongTypes - Free Generics Help By Email
    Join StrongTypes

    Community Credit Winner

    View my MVP profile

    The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

    Subscribe to Rolog via My AOL
    Subscribe to Rolog via Bloglines


    Subscribe to Rolog via Feedburner

    Furl Rolog




    Subscribe to Rolog via Newsburst


    Subscribe to Rolog via Pluck

    Add Rolog to Technorati Favorites




    Thycotic Secret Server

.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Kevin Jones

Thycotic Bloggers

  • Bryant Smith
  • John Morales
  • Jonathan Cogley

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

GridView Sorting

I discovered that the GridView lacks afeature when it comes to sorting. What if you are using a DataSourcesuch as SqlDataSource, but you don't want to sort the GridView usingthe same SelectCommand statement that was used to bind the GridView toits datasource? Using the OnSorting event of the GridView, you can addthe below to execute when the OnSorting event is fired:

VB.NET
Protected Sub gridview_Sorting( ByVal
sender As Object , ByVal e As GridViewSortEventArgs )
   Dim
strSortDir As String = Nothing

   Select Case e.SortDirection
      Case SortDirection
.Ascending
         strSortDir =
"ASC"


      Case SortDirection
.Descending
         strSortDir =
"DESC"

  
End Select

   sqlDataSource.SelectCommand = "SET NOCOUNT ON;SELECT Row FROM Table WHERE SomeCondition='Something' ORDER BY " & e.SortExpression & " " & strSortDir
End Sub

C#
protected
void gridview_Sorting( object sender, GridViewSortEventArgs
e)
{
   string strSortDir = null ;

   switch (e.SortDirection)
   {
      case SortDirection
.Ascending:
         strSortDir = "ASC"
;
         break
;

      case SortDirection
.Descending:
         strSortDir = "DESC"
;
         break
;
   }

   sqlDataSource.SelectCommand = "SET NOCOUNT ON;SELECT Row FROM Table WHERE SomeCondition='Something' ORDER BY " + e.SortExpression + " " + strSortDir;
}

Note: sqlDataSource is the ID of the SqlDataSource control.

R

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Thursday, December 01, 2005 9:25 PMby Ryan Olshan
Filed under: ,

Comments

No Comments

New Comments to this post are disabled