How to sort and page gridview

Last post 01-22-2008 3:06 AM by kem06.net. 5 replies.

Sort Posts:

  • How to sort and page gridview

    01-21-2008, 2:59 AM
    • Member
      56 point Member
    • kem06.net
    • Member since 12-11-2007, 2:58 AM
    • Posts 306

    I have a GridView and a datasource. The only properties i set to datasource are the ConnectionStrings, ProviderName, runat and ID as shown below. And according to a criteria i set SelectCommand property of my datasource on my .cs file of .aspx on which exists my gidview. My problem is that; although i set AllowPaging and AllowSorting properties of my gridview to true, it nor sorts or pages the gridview but just disappears. I just wonder how i could get over this?

    <asp:SqlDataSource ID="DSourceSirketSec" runat="server" ConnectionString="<%$ ConnectionStrings:Constring %>"

    ProviderName="<%$ ConnectionStrings:Constring .ProviderName %>">

    </asp:SqlDataSource>

    Thanks...

  • Re: How to sort and page gridview

    01-21-2008, 3:22 AM
    • All-Star
      22,266 point All-Star
    • vik20000in
    • Member since 12-30-2005, 6:02 AM
    • Kolkata
    • Posts 3,464
    • TrustedFriends-MVPs

    are u setting the select command in the page load inside if(!Ispostback) condition. if yes than that is the problem

    Vikram
    www.vikramlakhotia.com


    Please mark the answer if it helped you
  • Re: How to sort and page gridview

    01-21-2008, 3:54 AM
    • Member
      56 point Member
    • kem06.net
    • Member since 12-11-2007, 2:58 AM
    • Posts 306

    vik20000in:

    are u setting the select command in the page load inside if(!Ispostback) condition. if yes than that is the problem

    No actually, i set the selectcommand in click event of a button.

  • Re: How to sort and page gridview

    01-21-2008, 5:18 AM
    Answer
    • Member
      649 point Member
    • hspharic
    • Member since 02-27-2007, 7:18 AM
    • Erode,Tamilnadu - India
    • Posts 119

     

    Hi,

    Try this,

    In aspx file,

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"

    ProviderName="<%$ ConnectionStrings:pubsConnectionString.providerName %>" ></asp:SqlDataSource>

    <asp:Button ID="Button1" runat="server" OnClick="BtnSelect_Click" Text="Button" /><br />

    <asp:GridView ID="GridView1" runat="server" AllowPaging="true" AllowSorting="true" OnPageIndexChanging="GridView1_PageIndexChanging" OnSorting="GridView1_Sorting">

    </asp:GridView>

    In code behind,

    protected void BtnSelect_Click(object sender, EventArgs e)

    {

    BindGrid();

     

    }

    private void BindGrid()

    {

    string sort = "";

    if (ViewState["sortExpr"] + "" != "") sort = "order by " + ViewState["sortExpr"] + " " + ViewState["sortDir"];

    SqlDataSource1.SelectCommand = "select * from authors " + sort;

    GridView1.DataSource = SqlDataSource1;

    GridView1.DataBind();

    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)

    {

    string sort = "Asc";if(ViewState["sortExpr"] + "" != "" && ViewState["sortExpr"] + "" == e.SortExpression)

    {

    if(ViewState["sortDir"] + "" == "Asc")

    sort = "Desc";

    }

    ViewState[
    "sortExpr"] = e.SortExpression;ViewState["sortDir"] = sort;

     

    BindGrid();

    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

    GridView1.PageIndex = e.NewPageIndex;

    BindGrid();

    }

    Hariharasudhan Chandiramurthy


    Click Answered when you satisfied my replies.
  • Re: How to sort and page gridview

    01-21-2008, 7:44 AM
    • All-Star
      91,748 point All-Star
    • vinz
    • Member since 10-05-2007, 11:47 AM
    • Cebu, Philippines
    • Posts 13,769
    • TrustedFriends-MVPs

    Check this

    http://quickstarts.asp.net/QuickStartv20/util/srcview.aspx?path=~/aspnet/samples/data/GridViewSorting.src 

    "Code,Beer and Music ~ my way of being a programmer"

  • Re: How to sort and page gridview

    01-22-2008, 3:06 AM
    • Member
      56 point Member
    • kem06.net
    • Member since 12-11-2007, 2:58 AM
    • Posts 306

    Thanks hspharic, it works fine...

Page 1 of 1 (6 items)