Help with simple datagrid paging and sorting issue

Discussion in 'ASP .Net Datagrid Control' started by Charlie Kunkel, Apr 22, 2004.

  1. Please help!

    I have a simple web app that displays some search fields, posts back
    to itself onclick of the search button, and shows the results in a
    datagrid. I have default paging turned on, and SortExpressions set for
    all the datagrid columns. Yet, any time OnPageIndexChanged or
    OnSortCommand execute, i get either an empty datagrid, or the
    ever-annoying "Invalid CurrentPageIndex value. It must be >= 0 and <
    the PageCount." error. I can't figure out WHY!

    Here's the relevant code:


    using ...

    private void Page_Load(object sender, System.EventArgs e)
    {
    sqlConnection.Open();
    if (!Page.IsPostBack)
    {
    //populate dropdowns
    }

    }

    private void btnSearch_Click(object sender, System.EventArgs e)
    {

    #region "Set Command Object Parameters equal to current form
    values"
    sqlDataAdapter1.SelectCommand.Parameters[1].Value =
    txtRoadName.Text.ToString();//RoadName
    ...
    #endregion

    sqlDataAdapter1.Fill(dataSet11);
    DataGrid1.CurrentPageIndex = 0;
    BindData();
    }

    protected void BindData()
    {
    //dataSet11.Clear();
    //dataSet11.AcceptChanges();
    DataGrid1.DataSource = dataSet11.Tables[0].DefaultView;
    DataGrid1.DataBind();
    sqlConnection.Close();
    }

    protected void Sort (object sender, DataGridSortCommandEventArgs e)
    {
    //sort the rows in the DataView in the specifiec order
    dataSet11.Tables[0].DefaultView.Sort = e.SortExpression + " ASC";
    BindData();
    }


    protected void ChangeGridPage(object sender,
    DataGridPageChangedEventArgs e)
    {
    DataGrid1.CurrentPageIndex = e.NewPageIndex;
    BindData();
    }
     
    Charlie Kunkel, Apr 22, 2004
    #1
    1. Advertising

  2. Charlie Kunkel

    Juliet Choy Guest

    Hi Charlie,

    This great article from Scott Mitchell had mentioned this problem:
    http://msdn.microsoft.com/asp.net/u...en-us/dnaspp/html/aspnet-pageablesortable.asp

    That you need to set CurrentPageIndex = 0 in your sort handler.
    Hope this help.

    Regards,
    Juliet Choy
    Hong Kong
    Microsoft MVP - ASP.NET

    Charlie Kunkel wrote:

    > Please help!
    >
    > I have a simple web app that displays some search fields, posts back
    > to itself onclick of the search button, and shows the results in a
    > datagrid. I have default paging turned on, and SortExpressions set for
    > all the datagrid columns. Yet, any time OnPageIndexChanged or
    > OnSortCommand execute, i get either an empty datagrid, or the
    > ever-annoying "Invalid CurrentPageIndex value. It must be >= 0 and <
    > the PageCount." error. I can't figure out WHY!
    >
    > Here's the relevant code:
    >
    >
    > using ...
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > sqlConnection.Open();
    > if (!Page.IsPostBack)
    > {
    > //populate dropdowns
    > }
    >
    > }
    >
    > private void btnSearch_Click(object sender, System.EventArgs e)
    > {
    >
    > #region "Set Command Object Parameters equal to current form
    > values"
    > sqlDataAdapter1.SelectCommand.Parameters[1].Value =
    > txtRoadName.Text.ToString();//RoadName
    > ...
    > #endregion
    >
    > sqlDataAdapter1.Fill(dataSet11);
    > DataGrid1.CurrentPageIndex = 0;
    > BindData();
    > }
    >
    > protected void BindData()
    > {
    > //dataSet11.Clear();
    > //dataSet11.AcceptChanges();
    > DataGrid1.DataSource = dataSet11.Tables[0].DefaultView;
    > DataGrid1.DataBind();
    > sqlConnection.Close();
    > }
    >
    > protected void Sort (object sender, DataGridSortCommandEventArgs e)
    > {
    > //sort the rows in the DataView in the specifiec order
    > dataSet11.Tables[0].DefaultView.Sort = e.SortExpression + " ASC";
    > BindData();
    > }
    >
    >
    > protected void ChangeGridPage(object sender,
    > DataGridPageChangedEventArgs e)
    > {
    > DataGrid1.CurrentPageIndex = e.NewPageIndex;
    > BindData();
    > }
     
    Juliet Choy, Apr 23, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mo
    Replies:
    1
    Views:
    4,195
    FFlannery
    Jan 30, 2004
  2. wh1974
    Replies:
    0
    Views:
    2,439
    wh1974
    Jan 12, 2005
  3. Red
    Replies:
    1
    Views:
    723
  4. Kuldeep

    Datagrid: Sorting and Paging

    Kuldeep, May 8, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    354
    Kuldeep
    May 8, 2006
  5. JohnE

    sorting and paging issue

    JohnE, Aug 20, 2009, in forum: ASP .Net
    Replies:
    6
    Views:
    416
    JohnE
    Aug 22, 2009
Loading...

Share This Page