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

Reply via email to