• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 670
  • Last Modified:

sort

Hi,

Presently user clicks the header and it arrange to ascending order, how can we make that when the user clicks again o nthe same column it would now do descending?

Thanks.

    Sub Sort_Grid(sender As Object, e As DataGridSortCommandEventArgs)
        Dim SortExpression As String
        SortExpression = e.SortExpression.ToString()
        Dim odataView as DataView = GenerateSource().DefaultView
         odataView.Sort=SortExpression
         datagrid1.DataSource=odataView
        datagrid1.DataBind()
    End Sub 'Sort_Grid
0
lynnton
Asked:
lynnton
  • 5
  • 4
  • 2
2 Solutions
 
Ramesh SrinivasTechnical ConsultantCommented:
Set up a sort event like so...

Sub Datagrid1_Sort(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand

        ' Check to see if the column we clicked on is the current column defined
        ' in the sortCriteria property.
        If Me.sortCriteria = e.SortExpression Then
            If Me.sortDir = "desc" Then
                Me.sortDir = "asc"
            Else
                Me.sortDir = "desc"
            End If
        End If

        ' Assign the column clicked to the sortCriteria property
        Me.sortCriteria = e.SortExpression

        ' Now call the procedure to re-display the Data Grid
    End Sub
0
 
lynntonAuthor Commented:
saleek,

Is it okay if you could kindly guide me with the poper integration from the below to use your format?

Thanks.

Sub Sort_Grid(sender As Object, e As DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
        Dim SortExpression As String

 If Me.sortCriteria = e.SortExpression Then
            If Me.sortDir = "desc" Then
                Me.sortDir = "asc"
            Else
                Me.sortDir = "desc"
            End If
        End If

        SortExpression = e.SortExpression.ToString()
        Dim odataView as DataView = GenerateSource().DefaultView
         odataView.Sort=SortExpression
         datagrid1.DataSource=odataView
        datagrid1.DataBind()
    End Sub 'Sort_Grid

0
 
tusharashahCommented:
Declare a variable with your other class declaration as shared ( i think that is equivalent to Static in C#):

Public Shared SortMode As String = "DESC"

Now modify your Sub like following;

Sub Sort_Grid(sender As Object, e As DataGridSortCommandEventArgs)
        Dim SortExpression As String
        SortExpression = e.SortExpression.ToString()
'---------- Add this portion in your code --------------
        If SortMode = "Desc" Then
             SortMode = "Asc"
             SortExpression = SortExpression + " " + "Desc"
        End If  
'----------
        Dim odataView as DataView = GenerateSource().DefaultView
         odataView.Sort=SortExpression
         datagrid1.DataSource=odataView
        datagrid1.DataBind()
    End Sub 'Sort_Grid


-tushar
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
tusharashahCommented:
Little modification in code:

Public Shared SortMode As String = "Asc"

Now modify your Sub like following;

Sub Sort_Grid(sender As Object, e As DataGridSortCommandEventArgs)
        Dim SortExpression As String
        SortExpression = e.SortExpression.ToString()
'---------- Add this portion in your code --------------
        If SortMode = "Desc" Then
          SortMode = "Asc"
          SortExpression = SortExpression + " " + "Desc"
        Else
          SortMode = "Asc"
        End If
'----------
        Dim odataView as DataView = GenerateSource().DefaultView
         odataView.Sort=SortExpression
         datagrid1.DataSource=odataView
        datagrid1.DataBind()
    End Sub 'Sort_Grid

0
 
tusharashahCommented:
I'm sorry... prolly i need a coffee...

'---------- Add this portion in your code --------------
        If SortMode = "Desc" Then
          SortMode = "Asc"
          SortExpression = SortExpression + " " + "Desc"
        Else
          SortMode = "Desc"
        End If
'----------
0
 
lynntonAuthor Commented:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30451: Name 'SortMode' is not declared.
Source Error:
Line 63:             SortExpression = e.SortExpression.ToString()
Line 64:    
Line 65:             If SortMode = "Desc" Then
Line 66:               SortMode = "Asc"
Line 67:               SortExpression = SortExpression + " " + "Desc"
Source File: c:\inetpub\wwwroot\adherence\Repsched.aspx    Line: 65
0
 
tusharashahCommented:
DId you include this line with your Class Declaration?

Public Shared SortMode As String = "Asc"


-tushar
0
 
lynntonAuthor Commented:
I could make this one out, been trying lots of times to resolve this..


Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30188: Declaration expected.
Source Error:
Line 81:
Line 82:
Line 83:          datagrid1.DataSource=odataView
Line 84:         datagrid1.DataBind()
Line 85:     End Sub 'Sort_Grid
Source File: c:\inetpub\wwwroot\adherence\Repsched.aspx    Line: 83
0
 
lynntonAuthor Commented:
tusharashah,

Got it !!

there was multiple end sub.

Thanks so much for your time and patience.
0
 
tusharashahCommented:
Nice to have you going lynnton :)
0
 
Ramesh SrinivasTechnical ConsultantCommented:
Sorry lynnton, popped out for lunch! Glad you found a solution.

regards,

KS
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now