-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"></Alte rnatingIte mStyle>
<HeaderStyle Font-Size="8pt" Font-Bold="True" ForeColor="Black" BorderColor="#E7E7CF" BackColor="White"></Header Style>
<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_CO MP" HeaderText="% Complete"></asp:BoundColum n>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton id="cmdViewTasks" runat="server" CommandName="viewTasks">Vi ew 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("Project s") Is Nothing Then
For Each row In dsProjects.Tables("Project s").Rows
totalDur = dsProjects.Tables("Project s").Rows(i ndex).Item ("PERCENT_ COMP")
If totalDur > 0 Then
openTaskDur = CalcOpenDuration(dsProject s.Tables(" Projects") .Rows(inde x).Item("I D"))
percentComplete = (openTaskDur / totalDur) * 100
dsProjects.Tables("Project s").Rows(i ndex).Item ("PERCENT_ COMP") = percentComplete
End If
index += 1
Next
End If
dgProjects.DataSource = dsProjects.Tables("Project s")
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.
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
<AlternatingItemStyle Height="0px" BackColor="#E0E0E0"></Alte
<HeaderStyle Font-Size="8pt" Font-Bold="True" ForeColor="Black" BorderColor="#E7E7CF" BackColor="White"></Header
<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_CO
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton id="cmdViewTasks" runat="server" CommandName="viewTasks">Vi
</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("Project
For Each row In dsProjects.Tables("Project
totalDur = dsProjects.Tables("Project
If totalDur > 0 Then
openTaskDur = CalcOpenDuration(dsProject
percentComplete = (openTaskDur / totalDur) * 100
dsProjects.Tables("Project
End If
index += 1
Next
End If
dgProjects.DataSource = dsProjects.Tables("Project
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.
dsProjects.Sort = "Field DESC"
try again
that was for a dataview
dsProjects.Tables("Project s").Defaul tView.Sort = "fieldname DESC"
Replace fieldname with the column you want to sort by
that was for a dataview
dsProjects.Tables("Project
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
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 DataGridSortCommandEventAr gs) Handles dgProjects.SortCommand
End Sub
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 DataGridSortCommandEventAr
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
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
ASKER
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 DataGridSortCommandEventAr gs) Handles dgProjects.SortCommand
Dim timeFrame As Integer
Dim adpt As SqlClient.SqlDataAdapter
timeFrame = CType(rbtnLstTimeframe.Sel ectedValue , 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(_ projectReQ uey, dbConnection)
adpt.Fill(dsProjects, "Projects")
LoadProjects()
Case 1 'future projects
_projectReQuey = _projectReQuey & " WHERE (GETDATE() < P2.START_DATE) " & _
" ORDER " & E.SortExpression
adpt = New SqlClient.SqlDataAdapter(_ projectReQ uey, dbConnection)
adpt.Fill(dsProjects, "Projects")
LoadProjects()
Case -1 'past projects
_projectReQuey = _projectReQuey & " WHERE (GETDATE() > DUE_DATE) " & _
" ORDER " & E.SortExpression
adpt = New SqlClient.SqlDataAdapter(_ projectReQ uey, dbConnection)
adpt.Fill(dsProjects, "Projects")
LoadProjects()
End Select
End If
End Sub
any ideas how to make it bi -diectional
Sub SortCommand_OnClick(ByVal Source As Object, ByVal E As DataGridSortCommandEventAr
Dim timeFrame As Integer
Dim adpt As SqlClient.SqlDataAdapter
timeFrame = CType(rbtnLstTimeframe.Sel
'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(_
adpt.Fill(dsProjects, "Projects")
LoadProjects()
Case 1 'future projects
_projectReQuey = _projectReQuey & " WHERE (GETDATE() < P2.START_DATE) " & _
" ORDER " & E.SortExpression
adpt = New SqlClient.SqlDataAdapter(_
adpt.Fill(dsProjects, "Projects")
LoadProjects()
Case -1 'past projects
_projectReQuey = _projectReQuey & " WHERE (GETDATE() > DUE_DATE) " & _
" ORDER " & E.SortExpression
adpt = New SqlClient.SqlDataAdapter(_
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
ASKER
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(q uery, dbConnection)
adpt.Fill(dsProjects, "Projects")
dgProjects.DataSource = dsProjects.Tables("Project s")
dgProjects.DataBind()
this is how i am doing it, maybe its wrong.
query = "blah blah blah "
adpt = New SqlClient.SqlDataAdapter(q
adpt.Fill(dsProjects, "Projects")
dgProjects.DataSource = dsProjects.Tables("Project
dgProjects.DataBind()
query = "blah blah blah "
adpt = New SqlClient.SqlDataAdapter(q uery, dbConnection)
adpt.Fill(dsProjects, "Projects")
dgProjects.DataSource = dsProjects
dgProjects.DataMember = ("Projects")
this is how i do it on my datagrid and it works fine
adpt = New SqlClient.SqlDataAdapter(q
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
cry....... Now the datagrid doesnt appear