Datagrid paging I lose my sort

Discussion in 'ASP .Net Web Controls' started by cindy, Nov 7, 2005.

  1. cindy

    cindy Guest

    I have a ds from sql, datagrid has sorting true, 3 databound and 1 template
    column sort expression are the data values Does a sort and then I page to
    second pageand I lose the sort. How can I sort the data and then page the
    results without losing the sort. Also is there a way that when I do the sort
    I do not have to go back to the database for the select statement?
    --
    cindy
    cindy, Nov 7, 2005
    #1
    1. Advertising

  2. Hi Cindy,

    Welcome to ASPNET newsgroup.
    From your description, you're using the ASP.NET datagrid control to
    displaying some data from SQLserver, you've also provide paging and sorting
    function for the datagrid. However, currently you found that after sorting
    a certain bouldcolumn, if we change the page index , the new paged data is
    not sorted , yes?

    As for this problem, it is likely caused by the rebinding of the datagrid
    after we change the page index. As for ASP.NET 1.X datagrid, we need to
    perform databinding each time we change the page index or change sort
    column(expression). So when we first make a sort operation, the datagrid is
    binded with sorted datasource (generally it would be a sorted DataView...).
    However, if we continue to make a changing page index operation, again we
    have put the code the rebind the datagrid with the new page's data, yes?
    And thus, the problem is that whether the new page's data is also sorted?
    If it it not sorted , then we'll encounter the behavior you met. So is
    this the cause of your problem? If so, we need to make sure the new
    datasource of new page we bind to the datagrid is also sorted as the last
    sort command's sort expression. To do this, we can store a variable in
    Page's ViewState to identify the last sort expression and whenever we need
    to rebind the datagrid, we can sort the datasource through the sort
    expression stored previously...

    Hope helps. Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)




    --------------------
    | Thread-Topic: Datagrid paging I lose my sort
    | thread-index: AcXj7D2KqTsezqbkT324nyzRX02Whw==
    | X-WBNR-Posting-Host: 71.136.160.3
    | From: "=?Utf-8?B?Y2luZHk=?=" <>
    | Subject: Datagrid paging I lose my sort
    | Date: Mon, 7 Nov 2005 14:40:23 -0800
    | Lines: 7
    | Message-ID: <>
    | MIME-Version: 1.0
    | Content-Type: text/plain;
    | charset="Utf-8"
    | Content-Transfer-Encoding: 7bit
    | X-Newsreader: Microsoft CDO for Windows 2000
    | Content-Class: urn:content-classes:message
    | Importance: normal
    | Priority: normal
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
    | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
    | Xref: TK2MSFTNGXA01.phx.gbl
    microsoft.public.dotnet.framework.aspnet.webcontrols:11750
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    |
    | I have a ds from sql, datagrid has sorting true, 3 databound and 1
    template
    | column sort expression are the data values Does a sort and then I page
    to
    | second pageand I lose the sort. How can I sort the data and then page
    the
    | results without losing the sort. Also is there a way that when I do the
    sort
    | I do not have to go back to the database for the select statement?
    | --
    | cindy
    |
    Steven Cheng[MSFT], Nov 8, 2005
    #2
    1. Advertising

  3. cindy

    cindy Guest

    do i call save page view state before I rebind? this is problem, is the
    variable the data field of the column name? is there a web link somewhere
    for this? others must have it.
    --
    cindy


    "Steven Cheng[MSFT]" wrote:

    > Hi Cindy,
    >
    > Welcome to ASPNET newsgroup.
    > From your description, you're using the ASP.NET datagrid control to
    > displaying some data from SQLserver, you've also provide paging and sorting
    > function for the datagrid. However, currently you found that after sorting
    > a certain bouldcolumn, if we change the page index , the new paged data is
    > not sorted , yes?
    >
    > As for this problem, it is likely caused by the rebinding of the datagrid
    > after we change the page index. As for ASP.NET 1.X datagrid, we need to
    > perform databinding each time we change the page index or change sort
    > column(expression). So when we first make a sort operation, the datagrid is
    > binded with sorted datasource (generally it would be a sorted DataView...).
    > However, if we continue to make a changing page index operation, again we
    > have put the code the rebind the datagrid with the new page's data, yes?
    > And thus, the problem is that whether the new page's data is also sorted?
    > If it it not sorted , then we'll encounter the behavior you met. So is
    > this the cause of your problem? If so, we need to make sure the new
    > datasource of new page we bind to the datagrid is also sorted as the last
    > sort command's sort expression. To do this, we can store a variable in
    > Page's ViewState to identify the last sort expression and whenever we need
    > to rebind the datagrid, we can sort the datasource through the sort
    > expression stored previously...
    >
    > Hope helps. Thanks,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    >
    >
    >
    > --------------------
    > | Thread-Topic: Datagrid paging I lose my sort
    > | thread-index: AcXj7D2KqTsezqbkT324nyzRX02Whw==
    > | X-WBNR-Posting-Host: 71.136.160.3
    > | From: "=?Utf-8?B?Y2luZHk=?=" <>
    > | Subject: Datagrid paging I lose my sort
    > | Date: Mon, 7 Nov 2005 14:40:23 -0800
    > | Lines: 7
    > | Message-ID: <>
    > | MIME-Version: 1.0
    > | Content-Type: text/plain;
    > | charset="Utf-8"
    > | Content-Transfer-Encoding: 7bit
    > | X-Newsreader: Microsoft CDO for Windows 2000
    > | Content-Class: urn:content-classes:message
    > | Importance: normal
    > | Priority: normal
    > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
    > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    > | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
    > | Xref: TK2MSFTNGXA01.phx.gbl
    > microsoft.public.dotnet.framework.aspnet.webcontrols:11750
    > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    > |
    > | I have a ds from sql, datagrid has sorting true, 3 databound and 1
    > template
    > | column sort expression are the data values Does a sort and then I page
    > to
    > | second pageand I lose the sort. How can I sort the data and then page
    > the
    > | results without losing the sort. Also is there a way that when I do the
    > sort
    > | I do not have to go back to the database for the select statement?
    > | --
    > | cindy
    > |
    >
    >
    cindy, Nov 8, 2005
    #3
  4. Hi Cindy,

    ASP.NET page will persist ViewState between mutiple postback requests. So
    we can just store the latest Sorted Column's expression into ViewState like:

    private void DataGrid1_SortCommand(object source,
    System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
    string sortexp = e.SortExpression;

    this.ViewState["DataGrid1_SortExpression"] = sortexp;
    }


    Then, next time, when in the DataGrid1's PageIndexChanged event, we should
    do the following:

    private void DataGrid1_PageIndexChanged(object source,
    System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    string sortexp = this.ViewState["DataGrid1_SortExpression"] as string;

    //GetPagedDataSource(index) is just a function
    //we used to retrieve data for a certain page
    DataView dv = GetPagedDataSource(e.NewPageIndex);

    dv.Sort = sortexp;

    //rebind datagrid here....

    }

    Also, if there're any other postback events where we need to rebind the
    DataGrid, we also need to specify the correct Sort Expression for the
    DataView ( or get the correct Sorted DataCollection)

    If there're anything unclear, please feel free to post here. Thanks,


    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)



    --------------------
    | Thread-Topic: Datagrid paging I lose my sort
    | thread-index: AcXkiy9OiSAovRdvQBqQ6Uh7zDa2Lw==
    | X-WBNR-Posting-Host: 71.136.160.120
    | From: "=?Utf-8?B?Y2luZHk=?=" <>
    | References: <>
    <>
    | Subject: RE: Datagrid paging I lose my sort
    | Date: Tue, 8 Nov 2005 09:38:09 -0800
    | Lines: 85
    | Message-ID: <>
    | MIME-Version: 1.0
    | Content-Type: text/plain;
    | charset="Utf-8"
    | Content-Transfer-Encoding: 7bit
    | X-Newsreader: Microsoft CDO for Windows 2000
    | Content-Class: urn:content-classes:message
    | Importance: normal
    | Priority: normal
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
    | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl
    | Xref: TK2MSFTNGXA01.phx.gbl
    microsoft.public.dotnet.framework.aspnet.webcontrols:11768
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    |
    | do i call save page view state before I rebind? this is problem, is the
    | variable the data field of the column name? is there a web link
    somewhere
    | for this? others must have it.
    | --
    | cindy
    |
    |
    | "Steven Cheng[MSFT]" wrote:
    |
    | > Hi Cindy,
    | >
    | > Welcome to ASPNET newsgroup.
    | > From your description, you're using the ASP.NET datagrid control to
    | > displaying some data from SQLserver, you've also provide paging and
    sorting
    | > function for the datagrid. However, currently you found that after
    sorting
    | > a certain bouldcolumn, if we change the page index , the new paged data
    is
    | > not sorted , yes?
    | >
    | > As for this problem, it is likely caused by the rebinding of the
    datagrid
    | > after we change the page index. As for ASP.NET 1.X datagrid, we need to
    | > perform databinding each time we change the page index or change sort
    | > column(expression). So when we first make a sort operation, the
    datagrid is
    | > binded with sorted datasource (generally it would be a sorted
    DataView...).
    | > However, if we continue to make a changing page index operation, again
    we
    | > have put the code the rebind the datagrid with the new page's data,
    yes?
    | > And thus, the problem is that whether the new page's data is also
    sorted?
    | > If it it not sorted , then we'll encounter the behavior you met. So
    is
    | > this the cause of your problem? If so, we need to make sure the new
    | > datasource of new page we bind to the datagrid is also sorted as the
    last
    | > sort command's sort expression. To do this, we can store a variable in
    | > Page's ViewState to identify the last sort expression and whenever we
    need
    | > to rebind the datagrid, we can sort the datasource through the sort
    | > expression stored previously...
    | >
    | > Hope helps. Thanks,
    | >
    | > Steven Cheng
    | > Microsoft Online Support
    | >
    | > Get Secure! www.microsoft.com/security
    | > (This posting is provided "AS IS", with no warranties, and confers no
    | > rights.)
    | >
    | >
    | >
    | >
    | > --------------------
    | > | Thread-Topic: Datagrid paging I lose my sort
    | > | thread-index: AcXj7D2KqTsezqbkT324nyzRX02Whw==
    | > | X-WBNR-Posting-Host: 71.136.160.3
    | > | From: "=?Utf-8?B?Y2luZHk=?=" <>
    | > | Subject: Datagrid paging I lose my sort
    | > | Date: Mon, 7 Nov 2005 14:40:23 -0800
    | > | Lines: 7
    | > | Message-ID: <>
    | > | MIME-Version: 1.0
    | > | Content-Type: text/plain;
    | > | charset="Utf-8"
    | > | Content-Transfer-Encoding: 7bit
    | > | X-Newsreader: Microsoft CDO for Windows 2000
    | > | Content-Class: urn:content-classes:message
    | > | Importance: normal
    | > | Priority: normal
    | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
    | > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    | > | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    | > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
    | > | Xref: TK2MSFTNGXA01.phx.gbl
    | > microsoft.public.dotnet.framework.aspnet.webcontrols:11750
    | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    | > |
    | > | I have a ds from sql, datagrid has sorting true, 3 databound and 1
    | > template
    | > | column sort expression are the data values Does a sort and then I
    page
    | > to
    | > | second pageand I lose the sort. How can I sort the data and then
    page
    | > the
    | > | results without losing the sort. Also is there a way that when I do
    the
    | > sort
    | > | I do not have to go back to the database for the select statement?
    | > | --
    | > | cindy
    | > |
    | >
    | >
    |
    Steven Cheng[MSFT], Nov 9, 2005
    #4
  5. Hi Cindy,

    How are you doing on this issue or does my further response help a little?
    If there're anything else we can help, please feel free to post here.

    Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    --------------------
    | X-Tomcat-ID: 159045279
    | References: <>
    <>
    <>
    | MIME-Version: 1.0
    | Content-Type: text/plain
    | Content-Transfer-Encoding: 7bit
    | From: (Steven Cheng[MSFT])
    | Organization: Microsoft
    | Date: Wed, 09 Nov 2005 08:48:21 GMT
    | Subject: RE: Datagrid paging I lose my sort
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    | Message-ID: <51$#>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    | Lines: 155
    | Path: TK2MSFTNGXA02.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet.webcontrols:31023
    | NNTP-Posting-Host: tomcatimport2.phx.gbl 10.201.218.182
    |
    | Hi Cindy,
    |
    | ASP.NET page will persist ViewState between mutiple postback requests. So
    | we can just store the latest Sorted Column's expression into ViewState
    like:
    |
    | private void DataGrid1_SortCommand(object source,
    | System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    | {
    | string sortexp = e.SortExpression;
    |
    | this.ViewState["DataGrid1_SortExpression"] = sortexp;
    | }
    |
    |
    | Then, next time, when in the DataGrid1's PageIndexChanged event, we
    should
    | do the following:
    |
    | private void DataGrid1_PageIndexChanged(object source,
    | System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    | {
    | string sortexp = this.ViewState["DataGrid1_SortExpression"] as string;
    |
    | //GetPagedDataSource(index) is just a function
    | //we used to retrieve data for a certain page
    | DataView dv = GetPagedDataSource(e.NewPageIndex);
    |
    | dv.Sort = sortexp;
    |
    | //rebind datagrid here....
    |
    | }
    |
    | Also, if there're any other postback events where we need to rebind the
    | DataGrid, we also need to specify the correct Sort Expression for the
    | DataView ( or get the correct Sorted DataCollection)
    |
    | If there're anything unclear, please feel free to post here. Thanks,
    |
    |
    | Steven Cheng
    | Microsoft Online Support
    |
    | Get Secure! www.microsoft.com/security
    | (This posting is provided "AS IS", with no warranties, and confers no
    | rights.)
    |
    |
    |
    | --------------------
    | | Thread-Topic: Datagrid paging I lose my sort
    | | thread-index: AcXkiy9OiSAovRdvQBqQ6Uh7zDa2Lw==
    | | X-WBNR-Posting-Host: 71.136.160.120
    | | From: "=?Utf-8?B?Y2luZHk=?=" <>
    | | References: <>
    | <>
    | | Subject: RE: Datagrid paging I lose my sort
    | | Date: Tue, 8 Nov 2005 09:38:09 -0800
    | | Lines: 85
    | | Message-ID: <>
    | | MIME-Version: 1.0
    | | Content-Type: text/plain;
    | | charset="Utf-8"
    | | Content-Transfer-Encoding: 7bit
    | | X-Newsreader: Microsoft CDO for Windows 2000
    | | Content-Class: urn:content-classes:message
    | | Importance: normal
    | | Priority: normal
    | | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
    | | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    | | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    | | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl
    | | Xref: TK2MSFTNGXA01.phx.gbl
    | microsoft.public.dotnet.framework.aspnet.webcontrols:11768
    | | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    | |
    | | do i call save page view state before I rebind? this is problem, is
    the
    | | variable the data field of the column name? is there a web link
    | somewhere
    | | for this? others must have it.
    | | --
    | | cindy
    | |
    | |
    | | "Steven Cheng[MSFT]" wrote:
    | |
    | | > Hi Cindy,
    | | >
    | | > Welcome to ASPNET newsgroup.
    | | > From your description, you're using the ASP.NET datagrid control to
    | | > displaying some data from SQLserver, you've also provide paging and
    | sorting
    | | > function for the datagrid. However, currently you found that after
    | sorting
    | | > a certain bouldcolumn, if we change the page index , the new paged
    data
    | is
    | | > not sorted , yes?
    | | >
    | | > As for this problem, it is likely caused by the rebinding of the
    | datagrid
    | | > after we change the page index. As for ASP.NET 1.X datagrid, we need
    to
    | | > perform databinding each time we change the page index or change sort
    | | > column(expression). So when we first make a sort operation, the
    | datagrid is
    | | > binded with sorted datasource (generally it would be a sorted
    | DataView...).
    | | > However, if we continue to make a changing page index operation,
    again
    | we
    | | > have put the code the rebind the datagrid with the new page's data,
    | yes?
    | | > And thus, the problem is that whether the new page's data is also
    | sorted?
    | | > If it it not sorted , then we'll encounter the behavior you met. So
    | is
    | | > this the cause of your problem? If so, we need to make sure the new
    | | > datasource of new page we bind to the datagrid is also sorted as the
    | last
    | | > sort command's sort expression. To do this, we can store a variable
    in
    | | > Page's ViewState to identify the last sort expression and whenever we
    | need
    | | > to rebind the datagrid, we can sort the datasource through the sort
    | | > expression stored previously...
    | | >
    | | > Hope helps. Thanks,
    | | >
    | | > Steven Cheng
    | | > Microsoft Online Support
    | | >
    | | > Get Secure! www.microsoft.com/security
    | | > (This posting is provided "AS IS", with no warranties, and confers no
    | | > rights.)
    | | >
    | | >
    | | >
    | | >
    | | > --------------------
    | | > | Thread-Topic: Datagrid paging I lose my sort
    | | > | thread-index: AcXj7D2KqTsezqbkT324nyzRX02Whw==
    | | > | X-WBNR-Posting-Host: 71.136.160.3
    | | > | From: "=?Utf-8?B?Y2luZHk=?=" <>
    | | > | Subject: Datagrid paging I lose my sort
    | | > | Date: Mon, 7 Nov 2005 14:40:23 -0800
    | | > | Lines: 7
    | | > | Message-ID: <>
    | | > | MIME-Version: 1.0
    | | > | Content-Type: text/plain;
    | | > | charset="Utf-8"
    | | > | Content-Transfer-Encoding: 7bit
    | | > | X-Newsreader: Microsoft CDO for Windows 2000
    | | > | Content-Class: urn:content-classes:message
    | | > | Importance: normal
    | | > | Priority: normal
    | | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
    | | > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    | | > | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    | | > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
    | | > | Xref: TK2MSFTNGXA01.phx.gbl
    | | > microsoft.public.dotnet.framework.aspnet.webcontrols:11750
    | | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    | | > |
    | | > | I have a ds from sql, datagrid has sorting true, 3 databound and 1
    | | > template
    | | > | column sort expression are the data values Does a sort and then I
    | page
    | | > to
    | | > | second pageand I lose the sort. How can I sort the data and then
    | page
    | | > the
    | | > | results without losing the sort. Also is there a way that when I
    do
    | the
    | | > sort
    | | > | I do not have to go back to the database for the select statement?
    | | > | --
    | | > | cindy
    | | > |
    | | >
    | | >
    | |
    |
    |
    Steven Cheng[MSFT], Nov 14, 2005
    #5
    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. Ady

    Lose position in datagrid

    Ady, Nov 13, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    538
    Guest
    Nov 14, 2003
  2. wh1974
    Replies:
    0
    Views:
    2,428
    wh1974
    Jan 12, 2005
  3. Red
    Replies:
    1
    Views:
    714
  4. =?Utf-8?B?VG9tIEg=?=
    Replies:
    0
    Views:
    496
    =?Utf-8?B?VG9tIEg=?=
    Sep 26, 2006
  5. Navin
    Replies:
    1
    Views:
    685
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page