Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid sorting - need help

Status
Not open for further replies.

DotNetGnat

Programmer
Mar 10, 2005
5,548
IN
ok here is psuedo code...

Code:
 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Not Page.IsPostBack Then
            BindData()
        End If
    End Sub

Sub BindData()
        If SortExp.Text = "" Then
            dgdata("ApprovalDate asc")
        Else
            dgdata(SortExp.Text)
        End If

    End Sub

Sub dgdata(ByVal SortField As String)
'code binding the data to the datagrid
end sub

  Sub SortCommand_OnClick(ByVal Sender As Object, ByVal E As DataGridSortCommandEventArgs)

        dgdata(SortOrder(E.SortExpression).ToString())

    End Sub

Function SortOrder(ByVal Field As String) As String

        Dim so As String = SortExp.Text
        If Field = so Then
            SortOrder = Replace(Field, "asc", "desc")
        Else
            SortOrder = Replace(Field, "desc", "asc")
        End If
    End Function

what am i doing wrong here...the sorting works on the first click but doesnt work on the second click... i wanted it to work both ways...it has got to do something with the post back thing or binding the data again...but not sure where to fix it in the code...

-DNG
 
I don't see where you are calling the Sort_Command function.

Jim
 
sort command function is in the datagrid on the aspx page...

<asp: datagrid ......>
....AllowSorting="True" OnSortCommand="SortCommand_OnClick">

-DNG
 
What do you mean it does not work on the second click? Is it just not sorting, or do you not see any data in the grid?
 
i see the datagrid and everything works fine...it just doesnt sort the next time...i mean when the datagrid is initially displayed on the pageload, it is sorted by the default...when i click on some other column it works fine and sorts ok...and when i click the same column again thinking it will sort the other way round...it doesnt sort...i am thinking that i need to bind the datagrid data again?? do i??

-DNG
 
DNG,

We have done this dance in an earlier thread:

thread855-1130440

Jim :)
 
You need to check if your initial field is the current sort field if it is then switch direction from asc, to desc and vice versa.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top