e.SortDirection always ascending

880 views
Skip to first unread message

isaac2004

unread,
Jun 3, 2009, 4:45:06 PM6/3/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
i have looked online and found a lot of answers for this, but am a
little confused on how it works. i am trying to sort a gridview with
datasource controls and having a problem.

here is my aspx

<asp:GridView ID="PitcherGrid" runat="server" FooterStyle-Font-
Bold="true" AllowSorting="True"
AutoGenerateColumns="False" ShowFooter="True"
OnPageIndexChanging="gridView_PageIndexChanging"
OnSorting="gridView_Sorting">
<Columns>
<asp:TemplateField HeaderText="Year"
SortExpression="Year">
<ItemTemplate>
<asp:Label ID="Label21" runat="server"
Text='<%# Bind("Year") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
Totals
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


my handler

protected void gridView_Sorting(object sender,
GridViewSortEventArgs e)
{
DataTable dataTable = PitcherGrid.DataSource as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " +
ConvertSortDirectionToSql(e.SortDirection);
PitcherGrid.DataSource = dataView;
PitcherGrid.DataBind();
}

a helper function that i call

private string ConvertSortDirectionToSql(SortDirection
sortDirection)
{
string newSortDirection = String.Empty;
string strsort = ViewState["sort"].ToString();
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "DESC";
break;

case SortDirection.Descending:
newSortDirection = "ASC";
break;
}

return newSortDirection;
}

the issue that comes up is that e.SortDirection is always ascending,
it makes sense to store it in a session or viewstate, but i am
confused to where that goes. thanks for the help

Cerebrus

unread,
Jun 4, 2009, 1:46:49 AM6/4/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Well, for starters, the GridView automatically appends the "ASC" and
"DESC" alternately to the SortExpression, based on the current
SortDirection. I'm not sure why you are trying to do this manually.
Secondly, there seems to be no reason to re-bind the GridView to a
sorted DataView when this is already done internally by the
Datasource.

Finally, I don't see you storing the Viewstate variable anywhere, just
accessing it.
Reply all
Reply to author
Forward
0 new messages