locked
How to setup default sorting on Gridview RRS feed

  • Question

  • User1287634789 posted

    See the code below, the data is from a stored procedure, and order by id desc. When the page opens first time, the id is desc order on fist page, but after page, let's say click page 2, the id order becomes asc order, then go back to the first page, the id becomes asc order. How to setup the gridview id desc order by default. Thanks.

    <asp:GridView ID="myGridView" AutoGenerateColumns="false" PageSize="50" AllowPaging="true" OnPageIndexChanging="myGridView_PageIndexChanging"
    CssClass="table table-bordered table-condensed display" runat="server">
    <Columns>
        <asp:TemplateField HeaderText="No.">
            <ItemTemplate>
                <%# Container.DataItemIndex + 1 %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:HyperLinkField DataTextField="audit_request_id" HeaderText="No." DataNavigateUrlFields="id" DataNavigateUrlFormatString="~\MyPage.aspx?ID={0}" />
        <asp:BoundField DataField="fielda" HeaderText="fielda" />
        <asp:BoundField DataField="fieldb" HeaderText="fieldb" />
    </Columns>
    <PagerStyle CssClass="gridPager" />
    </asp:GridView>

    protected void myGridView_PageIndexChanging(object source, GridViewPageEventArgs e)
        {
            myGridView.PageIndex = e.NewPageIndex;
            DataTable Results = GetData();
            myGridView.DataSource = Results
        }

     protected void gvSearchResults_BindData()
        {
            myGridView.PageIndex = e.NewPageIndex;
            DataTable Results = GetData();
            myGridView.DataSource = Results
        }

    Wednesday, March 7, 2018 3:45 PM

Answers

All replies