Help with simple datagrid paging and sorting issue

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default Help with simple datagrid paging and sorting issue

    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 Guest

  2. Similar Questions and Discussions

    1. What's the easiest way to..user define class...datagrid..paging..sorting..
      I have a large number of user define class objects and want display in a datagrid and able to perform paging and column sorting. It seems using...
    2. Datagrid does not respond to any sorting, dataview filtering, or paging events
      This is my third VB.NET forum to try and help me with my problems and several people have failed so I am hoping someone here is a TRUE GURU on...
    3. Dynamic columns in DataGrid, Paging/Sorting and Data Caching
      Hi, I have a DataGrid that has no columns and that is populated with columns based on data obtained from DB during PageLoad in the following way:...
    4. DataGrid - sorting/paging problem
      Hi. I have created a datagrid (datagrid1) without any columns on a aspx page. Then aspx.vb adds columns from a database. It is somthing like...
    5. How to bring paging, sorting and hyperlinking functioanlities in a datagrid?
      Hi there, I just started asp.net code 3 days ago from now. I have a situation in one of my pages that i need to provide all three...
  3. #2

    Default Re: Help with simple datagrid paging and sorting issue

    Hi Charlie,

    This great article from Scott Mitchell had mentioned this problem:
    [url]http://msdn.microsoft.com/asp.net/using/building/webcontrols/default.aspx?pull=/library/en-us/dnaspp/html/aspnet-pageablesortable.asp[/url]

    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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139