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. On Jun 4, 1:45 am, isaac2004 <[email protected]> wrote: > 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
