Bi Directional Sort in a data grid.

Hello Experts,
I am trying to sort columns in a datagrid for asc and desc.
here is the ASP code
[code]
<asp:datagrid id="dgProjects" runat="server" ForeColor="#666600" Font-Size="10pt" Width="400px"
      AutoGenerateColumns="False" Height="116px" HorizontalAlign="Center" BackColor="White" BorderColor="#E7E7CF" AllowSorting="True" OnSortCommand="SortCommand_OnClick">
      <AlternatingItemStyle Height="0px" BackColor="#E0E0E0"></AlternatingItemStyle>
      <HeaderStyle Font-Size="8pt" Font-Bold="True" ForeColor="Black" BorderColor="#E7E7CF" BackColor="White"></HeaderStyle>
      <Columns>
            <asp:BoundColumn DataField="ID" SortExpression="ID" ReadOnly="True" HeaderText="Project ID"></asp:BoundColumn>
            <asp:BoundColumn DataField="NAME" SortExpression="NAME" HeaderText="Project Name"></asp:BoundColumn>
            <asp:BoundColumn DataField="PERCENT_COMP" SortExpression="PERCENT_COMP" HeaderText="% Complete"></asp:BoundColumn>
            <asp:TemplateColumn>
                  <ItemTemplate>
                        <asp:LinkButton id="cmdViewTasks" runat="server" CommandName="viewTasks">View Tasks</asp:LinkButton>
                  </ItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn>
                  <ItemTemplate>
                        <asp:LinkButton id="cmdDetails" CommandName="details" Runat="server" Visible="true">Project Details</asp:LinkButton>
                  </ItemTemplate>
            </asp:TemplateColumn>
      </Columns>
</asp:datagrid>

[/code]

and this is the code to i use to load the datagrid in the code behind
[code]
    Private Sub LoadProjects()
        Dim index As Integer
        Dim openTaskDur As Integer
        Dim totalDur As Integer
        Dim percentComplete As Integer
        Dim row As DataRow
        Dim dbTable As DataTable
        Dim dbColumn As DataColumn

        index = 0

        If Not dsProjects.Tables("Projects") Is Nothing Then
            For Each row In dsProjects.Tables("Projects").Rows

                totalDur = dsProjects.Tables("Projects").Rows(index).Item("PERCENT_COMP")
                If totalDur > 0 Then
                    openTaskDur = CalcOpenDuration(dsProjects.Tables("Projects").Rows(index).Item("ID"))
                    percentComplete = (openTaskDur / totalDur) * 100

                    dsProjects.Tables("Projects").Rows(index).Item("PERCENT_COMP") = percentComplete
                End If
                index += 1
            Next

        End If
        dgProjects.DataSource = dsProjects.Tables("Projects")
        dgProjects.DataBind()

    End Sub


[/code]
I've been stuck on this for a couple of days now pulling out my hair, i need some serious help.
-ZugZug-Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

davidrichardsonCommented:
dsProjects.Sort = "Field DESC"
0
davidrichardsonCommented:
try again
that was for a dataview
dsProjects.Tables("Projects").DefaultView.Sort = "fieldname DESC"
Replace fieldname with the column you want to sort by
0
davidrichardsonCommented:
If you click on the column header within the datagrid it will also sort the columns
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

-ZugZug-Author Commented:
I try clicking on the column headers and it doesn't sort =(
Sorry I wasn't clear when I first posted the question I am trying to sort it when the user clicks on one of the columns. ID   Name  %complete

I have an event handler in the code behind but I don't know what to put in it

Sub SortCommand_OnClick(ByVal Source As Object, ByVal E As DataGridSortCommandEventArgs) Handles dgProjects.SortCommand

       

End Sub
0
davidrichardsonCommented:
this event doent sort the columns
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsdatagridclasssortcommandtopic.asp

When you say column id do you meen the cell or the column header
0
-ZugZug-Author Commented:
ya, sorry i mean column headers but I got this so far and now it sorts, but it is not bi-directional

 Sub SortCommand_OnClick(ByVal Source As Object, ByVal E As DataGridSortCommandEventArgs) Handles dgProjects.SortCommand

        Dim timeFrame As Integer
        Dim adpt As SqlClient.SqlDataAdapter


        timeFrame = CType(rbtnLstTimeframe.SelectedValue, Integer)
        'To check What button is selected for the resorting of the projects
        If Not timeFrame.Equals(Nothing) Then
            Select Case timeFrame
                Case 0 'presnt projects

                    _projectReQuey = _projectReQuey & " WHERE (GETDATE() BETWEEN P2.START_DATE AND P2.DUE_DATE) " & _
                    " ORDER BY " & E.SortExpression
                    adpt = New SqlClient.SqlDataAdapter(_projectReQuey, dbConnection)
                    adpt.Fill(dsProjects, "Projects")
                    LoadProjects()

                Case 1 'future projects

                    _projectReQuey = _projectReQuey & " WHERE (GETDATE() < P2.START_DATE) " & _
                    " ORDER " & E.SortExpression
                    adpt = New SqlClient.SqlDataAdapter(_projectReQuey, dbConnection)
                    adpt.Fill(dsProjects, "Projects")
                    LoadProjects()

                Case -1 'past projects

                    _projectReQuey = _projectReQuey & " WHERE (GETDATE() > DUE_DATE) " & _
                    " ORDER " & E.SortExpression
                    adpt = New SqlClient.SqlDataAdapter(_projectReQuey, dbConnection)
                    adpt.Fill(dsProjects, "Projects")
                    LoadProjects()

            End Select

        End If

    End Sub

any ideas how to make it bi -diectional
0
davidrichardsonCommented:
the datagrid should sort without the need for this code, if you remove the code and just click on the column header its sorts in and direction
0
-ZugZug-Author Commented:
I wish it did, I took the code and it doesn't do anything ???  I don't understand
this is how i am doing it, maybe its wrong.

            query = "blah blah blah "
            adpt = New SqlClient.SqlDataAdapter(query, dbConnection)
            adpt.Fill(dsProjects, "Projects")
           dgProjects.DataSource = dsProjects.Tables("Projects")
           dgProjects.DataBind()
0
davidrichardsonCommented:
query = "blah blah blah "
            adpt = New SqlClient.SqlDataAdapter(query, dbConnection)
            adpt.Fill(dsProjects, "Projects")
            dgProjects.DataSource = dsProjects
            dgProjects.DataMember = ("Projects")

this is how i do it on my datagrid and it works fine

0
davidrichardsonCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
-ZugZug-Author Commented:
cry....... Now the datagrid doesnt appear
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.