Carl3003
asked on
GridView Sorting
Hi,
I guess i am missing something bc sorting does not work.
<asp:GridView ID="gridUserInfo" AllowPaging="True" AllowSorting="True" runat="server"
AutoGenerateColumns="False " CellPadding="4" DataKeyNames="UserID"
ForeColor="#333333" GridLines="None"
OnPageIndexChanging="gridU serInfo_Pa geIndexCha nging" OnSorting ="gridUserInfo_OnSorting" >
<PagerSettings PageButtonCount="15" Position="TopAndBottom" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField SortExpression="UserID" HeaderText="Select">
<ItemTemplate>
<a href="javascript: OpenNewWindow('users.aspx? userid=<%# Eval("UserID") %>','500','600','SD')"><%# Eval("UserID") %></a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FirstName" HeaderText="First Name" NullDisplayText="n/a">
<ItemStyle Font-Names="verdana" Font-Size="XX-Small" Width="66px" />
<HeaderStyle Font-Names="Verdana" Font-Size="XX-Small" Height="15px" HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="EmailId" HeaderText="Email">
<ItemStyle Font-Names="verdana" Font-Size="XX-Small" Width="100px" />
<HeaderStyle Font-Names="verdana" Font-Size="XX-Small" Height="15px" HorizontalAlign="Left" />
</asp:BoundField>
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" CssClass="TierBNBTitleFont " />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" Font-Names="verdana" Font-Size="XX-Small" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
Protected Sub gridUserInfo_OnSorting(ByV al sender As Object, ByVal e As GridViewSortEventArgs)
' Check to see if the column we clicked on is the current column defined
' in the sortCriteria property.
If Me.sortCriteria = e.SortExpression Then
If Me.sortDir = "desc" Then
Me.sortDir = "asc"
Else
Me.sortDir = "desc"
End If
End If
' Assign the column clicked to the sortCriteria property
Me.sortCriteria = e.SortExpression
gridUserInfo.DataSource = LoadData(Session("uId"))
gridUserInfo.DataBind()
End Sub
' Holds the column name to be sorted on...
Public Property sortCriteria() As String
Get
Return CStr(viewstate("sortCriter ia"))
End Get
Set(ByVal Value As String)
ViewState("sortCriteria") = Value
End Set
End Property
' Holds the direction to be sorted...
Public Property sortDir() As String
Get
Return CStr(ViewState("sortDir"))
End Get
Set(ByVal Value As String)
ViewState("sortDir") = Value
End Set
End Property
I guess i am missing something bc sorting does not work.
<asp:GridView ID="gridUserInfo" AllowPaging="True" AllowSorting="True" runat="server"
AutoGenerateColumns="False
ForeColor="#333333" GridLines="None"
OnPageIndexChanging="gridU
<PagerSettings PageButtonCount="15" Position="TopAndBottom" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField SortExpression="UserID" HeaderText="Select">
<ItemTemplate>
<a href="javascript: OpenNewWindow('users.aspx?
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FirstName" HeaderText="First Name" NullDisplayText="n/a">
<ItemStyle Font-Names="verdana" Font-Size="XX-Small" Width="66px" />
<HeaderStyle Font-Names="Verdana" Font-Size="XX-Small" Height="15px" HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="EmailId" HeaderText="Email">
<ItemStyle Font-Names="verdana" Font-Size="XX-Small" Width="100px" />
<HeaderStyle Font-Names="verdana" Font-Size="XX-Small" Height="15px" HorizontalAlign="Left" />
</asp:BoundField>
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" CssClass="TierBNBTitleFont
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" Font-Names="verdana" Font-Size="XX-Small" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
Protected Sub gridUserInfo_OnSorting(ByV
' Check to see if the column we clicked on is the current column defined
' in the sortCriteria property.
If Me.sortCriteria = e.SortExpression Then
If Me.sortDir = "desc" Then
Me.sortDir = "asc"
Else
Me.sortDir = "desc"
End If
End If
' Assign the column clicked to the sortCriteria property
Me.sortCriteria = e.SortExpression
gridUserInfo.DataSource = LoadData(Session("uId"))
gridUserInfo.DataBind()
End Sub
' Holds the column name to be sorted on...
Public Property sortCriteria() As String
Get
Return CStr(viewstate("sortCriter
End Get
Set(ByVal Value As String)
ViewState("sortCriteria") = Value
End Set
End Property
' Holds the direction to be sorted...
Public Property sortDir() As String
Get
Return CStr(ViewState("sortDir"))
End Get
Set(ByVal Value As String)
ViewState("sortDir") = Value
End Set
End Property
ASKER
It was not working for me. Is sorting for datagrid and gridview the same?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Private Sub DataGrid1_SortCommand(ByVa
viewstate.Add("sortfield",
If viewstate("sortdirection")
viewstate.Add("sortdirecti
Else
If viewstate("sortdirection")
viewstate("sortdirection")
Else
viewstate("sortdirection")
End If
End If
Session("lastColSorted") = e.SortExpression.ToString & " " & viewstate("sortdirection")
tblView.Sort = e.SortExpression.ToString & " " & viewstate("sortdirection")
DataGrid1.DataBind()
End Sub
Good luck!