Persisting the currently selected row on a GridView

Discussion in 'ASP .Net' started by =?Utf-8?B?RGljaw==?=, Jan 19, 2006.

  1. I have a GridView bound to an ObjectDataSource. I have a Button that calls
    GridView.DataBind. I want the row that is selected before the DataBind to
    still be selected afterwards.

    This happens automatically if the data doesn't change. But if records have
    been added or deleted then it looks as if some code is necessary: I've done
    this by using GridView.SelectedValue to get the key value of the currently
    selected Row and then by itterating through the DataKeys collection (after
    the DataBind has completed) I can work out the RowIndex of the Row that has
    the same key value. There's some more code to adjust for the GridView's
    PageIndex but otherwise this works.

    But what I've not been able to do is adjust for the occassions when the row
    that I'm hoping to select is no longer on the same PageIndex. I've got close
    by handling the ObjectDataSource_Selected event and I can now predict which
    page the row will be on (although I've not tested wither sort orders effect
    this). But how do I then make the GridView display the correct page?

    Hope you can help.
     
    =?Utf-8?B?RGljaw==?=, Jan 19, 2006
    #1
    1. Advertising

  2. Hi Richard,

    Welcome to ASPNET newsgroup.
    Regarding on the GridView control's row selection, it is a very simple
    implementation which dosn't quite associated with the underlying
    datasource, the SelectedIndex only range in the current displayed page of
    data. So when our page size is set to 5, the available selectedIndex is
    from 0 to 4....

    So when there is changes made on the underlying datasource or page index
    changed... If we still want to keep the original selected dataitem, I'm
    afraid we have to manually calculate the index of that record on the
    certain page (according to total records count, page size), then set the
    proper page index and selected index....

    Regards,

    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: Persisting the currently selected row on a GridView
    | thread-index: AcYdOIdSa2b3anygQnilL8UCN+aWPQ==
    | X-WBNR-Posting-Host: 193.130.25.218
    | From: "=?Utf-8?B?RGljaw==?=" <>
    | Subject: Persisting the currently selected row on a GridView
    | Date: Thu, 19 Jan 2006 12:40:05 -0800
    | Lines: 19
    | 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
    | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet:372201
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    |
    | I have a GridView bound to an ObjectDataSource. I have a Button that
    calls
    | GridView.DataBind. I want the row that is selected before the DataBind to
    | still be selected afterwards.
    |
    | This happens automatically if the data doesn't change. But if records
    have
    | been added or deleted then it looks as if some code is necessary: I've
    done
    | this by using GridView.SelectedValue to get the key value of the
    currently
    | selected Row and then by itterating through the DataKeys collection
    (after
    | the DataBind has completed) I can work out the RowIndex of the Row that
    has
    | the same key value. There's some more code to adjust for the GridView's
    | PageIndex but otherwise this works.
    |
    | But what I've not been able to do is adjust for the occassions when the
    row
    | that I'm hoping to select is no longer on the same PageIndex. I've got
    close
    | by handling the ObjectDataSource_Selected event and I can now predict
    which
    | page the row will be on (although I've not tested wither sort orders
    effect
    | this). But how do I then make the GridView display the correct page?
    |
    | Hope you can help.
    |
     
    Steven Cheng[MSFT], Jan 20, 2006
    #2
    1. Advertising

  3. Thanks for the reply. I'm almost certain I understood what you wrote. I think
    the (only?) place to calculate the page and index that the record will be on
    is the ObjectDataSource_Selected event because, as far as I can see, this is
    the only event that has access to the underlying datasource. But if I set
    GridView.PageIndex from this event it causes the control to repeat the call
    to the database, i.e. the series of events is thus:

    User selectes to "Refresh Bound Controls"...
    1. ObjectDataSource_Selecting
    2. ObjectDataSource_Selected
    2.1 Calculate and set the required page i.e. GridView.PageIndex = n
    3. GridView_DataBinding
    4. GridView_DataBound
    5. ObjectDataSource_Selecting
    6. ObjectDataSource_Selected
    7. GridView_DataBinding
    8. GridView_DataBound

    I think that events 5 to 8 are caused by changing the GridView.PageIndex in
    2.1. The events to not re-fire the second time GridView.PageIndex is set,
    presumably because the PageIndex is already the required value.

    The key thing here is that the database is called twice (and maybe the page
    is rendered on the BRowser twice?) and is thus an overhead that we should
    avoid. Can you help me with a better solution?


    "Steven Cheng[MSFT]" wrote:

    > Hi Richard,
    >
    > Welcome to ASPNET newsgroup.
    > Regarding on the GridView control's row selection, it is a very simple
    > implementation which dosn't quite associated with the underlying
    > datasource, the SelectedIndex only range in the current displayed page of
    > data. So when our page size is set to 5, the available selectedIndex is
    > from 0 to 4....
    >
    > So when there is changes made on the underlying datasource or page index
    > changed... If we still want to keep the original selected dataitem, I'm
    > afraid we have to manually calculate the index of that record on the
    > certain page (according to total records count, page size), then set the
    > proper page index and selected index....
    >
    > Regards,
    >
    > 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: Persisting the currently selected row on a GridView
    > | thread-index: AcYdOIdSa2b3anygQnilL8UCN+aWPQ==
    > | X-WBNR-Posting-Host: 193.130.25.218
    > | From: "=?Utf-8?B?RGljaw==?=" <>
    > | Subject: Persisting the currently selected row on a GridView
    > | Date: Thu, 19 Jan 2006 12:40:05 -0800
    > | Lines: 19
    > | 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
    > | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl
    > | Xref: TK2MSFTNGXA02.phx.gbl
    > microsoft.public.dotnet.framework.aspnet:372201
    > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    > |
    > | I have a GridView bound to an ObjectDataSource. I have a Button that
    > calls
    > | GridView.DataBind. I want the row that is selected before the DataBind to
    > | still be selected afterwards.
    > |
    > | This happens automatically if the data doesn't change. But if records
    > have
    > | been added or deleted then it looks as if some code is necessary: I've
    > done
    > | this by using GridView.SelectedValue to get the key value of the
    > currently
    > | selected Row and then by itterating through the DataKeys collection
    > (after
    > | the DataBind has completed) I can work out the RowIndex of the Row that
    > has
    > | the same key value. There's some more code to adjust for the GridView's
    > | PageIndex but otherwise this works.
    > |
    > | But what I've not been able to do is adjust for the occassions when the
    > row
    > | that I'm hoping to select is no longer on the same PageIndex. I've got
    > close
    > | by handling the ObjectDataSource_Selected event and I can now predict
    > which
    > | page the row will be on (although I've not tested wither sort orders
    > effect
    > | this). But how do I then make the GridView display the correct page?
    > |
    > | Hope you can help.
    > |
    >
    >
     
    =?Utf-8?B?RGljaw==?=, Jan 20, 2006
    #3
  4. Thanks for your response Richard,

    Yes, changing the Page will cause the GridView to rebind the data from
    DataSource control again.... I'm afraid this is not avoidable if the
    original selected data item is not on the current Page( or in anothe word,
    we have to change page index .....). Also, call the DataBind method twice
    won't make the GridView be rendered twice on the page. And the drawback is
    that the database will be queried twice (if not using cache) as you
    mentioned....

    Regards,

    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: Persisting the currently selected row on a GridView
    | thread-index: AcYdoU0ECG96QNn/Q3GGTxtnEgBdwQ==
    | X-WBNR-Posting-Host: 86.137.242.173
    | From: "=?Utf-8?B?RGljaw==?=" <>
    | References: <>
    <>
    | Subject: RE: Persisting the currently selected row on a GridView
    | Date: Fri, 20 Jan 2006 01:10:04 -0800
    | Lines: 112
    | 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
    | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet:372333
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    |
    | Thanks for the reply. I'm almost certain I understood what you wrote. I
    think
    | the (only?) place to calculate the page and index that the record will be
    on
    | is the ObjectDataSource_Selected event because, as far as I can see, this
    is
    | the only event that has access to the underlying datasource. But if I set
    | GridView.PageIndex from this event it causes the control to repeat the
    call
    | to the database, i.e. the series of events is thus:
    |
    | User selectes to "Refresh Bound Controls"...
    | 1. ObjectDataSource_Selecting
    | 2. ObjectDataSource_Selected
    | 2.1 Calculate and set the required page i.e. GridView.PageIndex = n
    | 3. GridView_DataBinding
    | 4. GridView_DataBound
    | 5. ObjectDataSource_Selecting
    | 6. ObjectDataSource_Selected
    | 7. GridView_DataBinding
    | 8. GridView_DataBound
    |
    | I think that events 5 to 8 are caused by changing the GridView.PageIndex
    in
    | 2.1. The events to not re-fire the second time GridView.PageIndex is set,
    | presumably because the PageIndex is already the required value.
    |
    | The key thing here is that the database is called twice (and maybe the
    page
    | is rendered on the BRowser twice?) and is thus an overhead that we should
    | avoid. Can you help me with a better solution?
    |
    |
    | "Steven Cheng[MSFT]" wrote:
    |
    | > Hi Richard,
    | >
    | > Welcome to ASPNET newsgroup.
    | > Regarding on the GridView control's row selection, it is a very simple
    | > implementation which dosn't quite associated with the underlying
    | > datasource, the SelectedIndex only range in the current displayed page
    of
    | > data. So when our page size is set to 5, the available selectedIndex is
    | > from 0 to 4....
    | >
    | > So when there is changes made on the underlying datasource or page
    index
    | > changed... If we still want to keep the original selected dataitem, I'm
    | > afraid we have to manually calculate the index of that record on the
    | > certain page (according to total records count, page size), then set
    the
    | > proper page index and selected index....
    | >
    | > Regards,
    | >
    | > 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: Persisting the currently selected row on a GridView
    | > | thread-index: AcYdOIdSa2b3anygQnilL8UCN+aWPQ==
    | > | X-WBNR-Posting-Host: 193.130.25.218
    | > | From: "=?Utf-8?B?RGljaw==?=" <>
    | > | Subject: Persisting the currently selected row on a GridView
    | > | Date: Thu, 19 Jan 2006 12:40:05 -0800
    | > | Lines: 19
    | > | 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
    | > | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    | > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl
    | > | Xref: TK2MSFTNGXA02.phx.gbl
    | > microsoft.public.dotnet.framework.aspnet:372201
    | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    | > |
    | > | I have a GridView bound to an ObjectDataSource. I have a Button that
    | > calls
    | > | GridView.DataBind. I want the row that is selected before the
    DataBind to
    | > | still be selected afterwards.
    | > |
    | > | This happens automatically if the data doesn't change. But if records
    | > have
    | > | been added or deleted then it looks as if some code is necessary:
    I've
    | > done
    | > | this by using GridView.SelectedValue to get the key value of the
    | > currently
    | > | selected Row and then by itterating through the DataKeys collection
    | > (after
    | > | the DataBind has completed) I can work out the RowIndex of the Row
    that
    | > has
    | > | the same key value. There's some more code to adjust for the
    GridView's
    | > | PageIndex but otherwise this works.
    | > |
    | > | But what I've not been able to do is adjust for the occassions when
    the
    | > row
    | > | that I'm hoping to select is no longer on the same PageIndex. I've
    got
    | > close
    | > | by handling the ObjectDataSource_Selected event and I can now predict
    | > which
    | > | page the row will be on (although I've not tested wither sort orders
    | > effect
    | > | this). But how do I then make the GridView display the correct page?
    | > |
    | > | Hope you can help.
    | > |
    | >
    | >
    |
     
    Steven Cheng[MSFT], Jan 23, 2006
    #4
  5. Thanks for your reply. Over the weekend I learnt about server-side sorting
    and paging and this eventually leads to a perfectly elegent solution for
    working out which page the required record will be on.

    "Steven Cheng[MSFT]" wrote:

    > Thanks for your response Richard,
    >
    > Yes, changing the Page will cause the GridView to rebind the data from
    > DataSource control again.... I'm afraid this is not avoidable if the
    > original selected data item is not on the current Page( or in anothe word,
    > we have to change page index .....). Also, call the DataBind method twice
    > won't make the GridView be rendered twice on the page. And the drawback is
    > that the database will be queried twice (if not using cache) as you
    > mentioned....
    >
    > Regards,
    >
    > 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: Persisting the currently selected row on a GridView
    > | thread-index: AcYdoU0ECG96QNn/Q3GGTxtnEgBdwQ==
    > | X-WBNR-Posting-Host: 86.137.242.173
    > | From: "=?Utf-8?B?RGljaw==?=" <>
    > | References: <>
    > <>
    > | Subject: RE: Persisting the currently selected row on a GridView
    > | Date: Fri, 20 Jan 2006 01:10:04 -0800
    > | Lines: 112
    > | 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
    > | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl
    > | Xref: TK2MSFTNGXA02.phx.gbl
    > microsoft.public.dotnet.framework.aspnet:372333
    > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    > |
    > | Thanks for the reply. I'm almost certain I understood what you wrote. I
    > think
    > | the (only?) place to calculate the page and index that the record will be
    > on
    > | is the ObjectDataSource_Selected event because, as far as I can see, this
    > is
    > | the only event that has access to the underlying datasource. But if I set
    > | GridView.PageIndex from this event it causes the control to repeat the
    > call
    > | to the database, i.e. the series of events is thus:
    > |
    > | User selectes to "Refresh Bound Controls"...
    > | 1. ObjectDataSource_Selecting
    > | 2. ObjectDataSource_Selected
    > | 2.1 Calculate and set the required page i.e. GridView.PageIndex = n
    > | 3. GridView_DataBinding
    > | 4. GridView_DataBound
    > | 5. ObjectDataSource_Selecting
    > | 6. ObjectDataSource_Selected
    > | 7. GridView_DataBinding
    > | 8. GridView_DataBound
    > |
    > | I think that events 5 to 8 are caused by changing the GridView.PageIndex
    > in
    > | 2.1. The events to not re-fire the second time GridView.PageIndex is set,
    > | presumably because the PageIndex is already the required value.
    > |
    > | The key thing here is that the database is called twice (and maybe the
    > page
    > | is rendered on the BRowser twice?) and is thus an overhead that we should
    > | avoid. Can you help me with a better solution?
    > |
    > |
    > | "Steven Cheng[MSFT]" wrote:
    > |
    > | > Hi Richard,
    > | >
    > | > Welcome to ASPNET newsgroup.
    > | > Regarding on the GridView control's row selection, it is a very simple
    > | > implementation which dosn't quite associated with the underlying
    > | > datasource, the SelectedIndex only range in the current displayed page
    > of
    > | > data. So when our page size is set to 5, the available selectedIndex is
    > | > from 0 to 4....
    > | >
    > | > So when there is changes made on the underlying datasource or page
    > index
    > | > changed... If we still want to keep the original selected dataitem, I'm
    > | > afraid we have to manually calculate the index of that record on the
    > | > certain page (according to total records count, page size), then set
    > the
    > | > proper page index and selected index....
    > | >
    > | > Regards,
    > | >
    > | > 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: Persisting the currently selected row on a GridView
    > | > | thread-index: AcYdOIdSa2b3anygQnilL8UCN+aWPQ==
    > | > | X-WBNR-Posting-Host: 193.130.25.218
    > | > | From: "=?Utf-8?B?RGljaw==?=" <>
    > | > | Subject: Persisting the currently selected row on a GridView
    > | > | Date: Thu, 19 Jan 2006 12:40:05 -0800
    > | > | Lines: 19
    > | > | 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
    > | > | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    > | > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl
    > | > | Xref: TK2MSFTNGXA02.phx.gbl
    > | > microsoft.public.dotnet.framework.aspnet:372201
    > | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    > | > |
    > | > | I have a GridView bound to an ObjectDataSource. I have a Button that
    > | > calls
    > | > | GridView.DataBind. I want the row that is selected before the
    > DataBind to
    > | > | still be selected afterwards.
    > | > |
    > | > | This happens automatically if the data doesn't change. But if records
    > | > have
    > | > | been added or deleted then it looks as if some code is necessary:
    > I've
    > | > done
    > | > | this by using GridView.SelectedValue to get the key value of the
    > | > currently
    > | > | selected Row and then by itterating through the DataKeys collection
    > | > (after
    > | > | the DataBind has completed) I can work out the RowIndex of the Row
    > that
    > | > has
    > | > | the same key value. There's some more code to adjust for the
    > GridView's
    > | > | PageIndex but otherwise this works.
    > | > |
    > | > | But what I've not been able to do is adjust for the occassions when
    > the
    > | > row
    > | > | that I'm hoping to select is no longer on the same PageIndex. I've
    > got
    > | > close
    > | > | by handling the ObjectDataSource_Selected event and I can now predict
    > | > which
    > | > | page the row will be on (although I've not tested wither sort orders
    > | > effect
    > | > | this). But how do I then make the GridView display the correct page?
    > | > |
    > | > | Hope you can help.
    > | > |
    > | >
    > | >
    > |
    >
    >
     
    =?Utf-8?B?RGljaw==?=, Jan 23, 2006
    #5
  6. Thanks for your quick response.

    Glad that you've also found your solution on this.

    Regards,

    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: Persisting the currently selected row on a GridView
    | thread-index: AcYgIfQ1A6LagImOS0eMeHlP0pDDSg==
    | X-WBNR-Posting-Host: 193.130.25.218
    | From: "=?Utf-8?B?RGljaw==?=" <>
    | References: <>
    <>
    <>
    <>
    | Subject: RE: Persisting the currently selected row on a GridView
    | Date: Mon, 23 Jan 2006 05:36:02 -0800
    | Lines: 183
    | 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
    | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA03.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet:372863
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    |
    | Thanks for your reply. Over the weekend I learnt about server-side
    sorting
    | and paging and this eventually leads to a perfectly elegent solution for
    | working out which page the required record will be on.
    |
    | "Steven Cheng[MSFT]" wrote:
    |
    | > Thanks for your response Richard,
    | >
    | > Yes, changing the Page will cause the GridView to rebind the data from
    | > DataSource control again.... I'm afraid this is not avoidable if the
    | > original selected data item is not on the current Page( or in anothe
    word,
    | > we have to change page index .....). Also, call the DataBind method
    twice
    | > won't make the GridView be rendered twice on the page. And the drawback
    is
    | > that the database will be queried twice (if not using cache) as you
    | > mentioned....
    | >
    | > Regards,
    | >
    | > 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: Persisting the currently selected row on a GridView
    | > | thread-index: AcYdoU0ECG96QNn/Q3GGTxtnEgBdwQ==
    | > | X-WBNR-Posting-Host: 86.137.242.173
    | > | From: "=?Utf-8?B?RGljaw==?=" <>
    | > | References: <>
    | > <>
    | > | Subject: RE: Persisting the currently selected row on a GridView
    | > | Date: Fri, 20 Jan 2006 01:10:04 -0800
    | > | Lines: 112
    | > | 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
    | > | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    | > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl
    | > | Xref: TK2MSFTNGXA02.phx.gbl
    | > microsoft.public.dotnet.framework.aspnet:372333
    | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    | > |
    | > | Thanks for the reply. I'm almost certain I understood what you wrote.
    I
    | > think
    | > | the (only?) place to calculate the page and index that the record
    will be
    | > on
    | > | is the ObjectDataSource_Selected event because, as far as I can see,
    this
    | > is
    | > | the only event that has access to the underlying datasource. But if I
    set
    | > | GridView.PageIndex from this event it causes the control to repeat
    the
    | > call
    | > | to the database, i.e. the series of events is thus:
    | > |
    | > | User selectes to "Refresh Bound Controls"...
    | > | 1. ObjectDataSource_Selecting
    | > | 2. ObjectDataSource_Selected
    | > | 2.1 Calculate and set the required page i.e. GridView.PageIndex =
    n
    | > | 3. GridView_DataBinding
    | > | 4. GridView_DataBound
    | > | 5. ObjectDataSource_Selecting
    | > | 6. ObjectDataSource_Selected
    | > | 7. GridView_DataBinding
    | > | 8. GridView_DataBound
    | > |
    | > | I think that events 5 to 8 are caused by changing the
    GridView.PageIndex
    | > in
    | > | 2.1. The events to not re-fire the second time GridView.PageIndex is
    set,
    | > | presumably because the PageIndex is already the required value.
    | > |
    | > | The key thing here is that the database is called twice (and maybe
    the
    | > page
    | > | is rendered on the BRowser twice?) and is thus an overhead that we
    should
    | > | avoid. Can you help me with a better solution?
    | > |
    | > |
    | > | "Steven Cheng[MSFT]" wrote:
    | > |
    | > | > Hi Richard,
    | > | >
    | > | > Welcome to ASPNET newsgroup.
    | > | > Regarding on the GridView control's row selection, it is a very
    simple
    | > | > implementation which dosn't quite associated with the underlying
    | > | > datasource, the SelectedIndex only range in the current displayed
    page
    | > of
    | > | > data. So when our page size is set to 5, the available
    selectedIndex is
    | > | > from 0 to 4....
    | > | >
    | > | > So when there is changes made on the underlying datasource or page
    | > index
    | > | > changed... If we still want to keep the original selected dataitem,
    I'm
    | > | > afraid we have to manually calculate the index of that record on
    the
    | > | > certain page (according to total records count, page size), then
    set
    | > the
    | > | > proper page index and selected index....
    | > | >
    | > | > Regards,
    | > | >
    | > | > 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: Persisting the currently selected row on a GridView
    | > | > | thread-index: AcYdOIdSa2b3anygQnilL8UCN+aWPQ==
    | > | > | X-WBNR-Posting-Host: 193.130.25.218
    | > | > | From: "=?Utf-8?B?RGljaw==?=" <>
    | > | > | Subject: Persisting the currently selected row on a GridView
    | > | > | Date: Thu, 19 Jan 2006 12:40:05 -0800
    | > | > | Lines: 19
    | > | > | 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
    | > | > | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    | > | > | Path:
    TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl
    | > | > | Xref: TK2MSFTNGXA02.phx.gbl
    | > | > microsoft.public.dotnet.framework.aspnet:372201
    | > | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    | > | > |
    | > | > | I have a GridView bound to an ObjectDataSource. I have a Button
    that
    | > | > calls
    | > | > | GridView.DataBind. I want the row that is selected before the
    | > DataBind to
    | > | > | still be selected afterwards.
    | > | > |
    | > | > | This happens automatically if the data doesn't change. But if
    records
    | > | > have
    | > | > | been added or deleted then it looks as if some code is necessary:
    | > I've
    | > | > done
    | > | > | this by using GridView.SelectedValue to get the key value of the
    | > | > currently
    | > | > | selected Row and then by itterating through the DataKeys
    collection
    | > | > (after
    | > | > | the DataBind has completed) I can work out the RowIndex of the
    Row
    | > that
    | > | > has
    | > | > | the same key value. There's some more code to adjust for the
    | > GridView's
    | > | > | PageIndex but otherwise this works.
    | > | > |
    | > | > | But what I've not been able to do is adjust for the occassions
    when
    | > the
    | > | > row
    | > | > | that I'm hoping to select is no longer on the same PageIndex.
    I've
    | > got
    | > | > close
    | > | > | by handling the ObjectDataSource_Selected event and I can now
    predict
    | > | > which
    | > | > | page the row will be on (although I've not tested wither sort
    orders
    | > | > effect
    | > | > | this). But how do I then make the GridView display the correct
    page?
    | > | > |
    | > | > | Hope you can help.
    | > | > |
    | > | >
    | > | >
    | > |
    | >
    | >
    |
     
    Steven Cheng[MSFT], Jan 23, 2006
    #6
    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. rodchar
    Replies:
    2
    Views:
    587
    rodchar
    Apr 16, 2008
  2. Mark B
    Replies:
    1
    Views:
    381
    Mark B
    Aug 27, 2008
  3. Want to add row under selected row of gridview

    , Feb 16, 2007, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    316
  4. Replies:
    4
    Views:
    244
  5. Selected Row Only GridView Row Visible?

    , Oct 17, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    307
Loading...

Share This Page