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

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.
0
-ZugZug-
Asked:
-ZugZug-
  • 7
  • 4
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
-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
 
-ZugZug-Author Commented:
cry....... Now the datagrid doesnt appear
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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