Link to home
Start Free TrialLog in
Avatar of -ZugZug-
-ZugZug-

asked on

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.
Avatar of davidrichardson
davidrichardson

dsProjects.Sort = "Field DESC"
try again
that was for a dataview
dsProjects.Tables("Projects").DefaultView.Sort = "fieldname DESC"
Replace fieldname with the column you want to sort by
If you click on the column header within the datagrid it will also sort the columns
Avatar of -ZugZug-

ASKER

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
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
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
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
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()
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

ASKER CERTIFIED SOLUTION
Avatar of davidrichardson
davidrichardson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
cry....... Now the datagrid doesnt appear