By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,805 Members | 1,100 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,805 IT Pros & Developers. It's quick & easy.

Persisting the currently selected row on a GridView

P: n/a
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.
Jan 19 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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==?=" <Ri***********@nospam.nospam>
| Subject: Persisting the currently selected row on a GridView
| Date: Thu, 19 Jan 2006 12:40:05 -0800
| Lines: 19
| Message-ID: <61**********************************@microsoft.co m>
| 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!TK2MSFT NGXA03.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.
|

Jan 20 '06 #2

P: n/a
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==?=" <Ri***********@nospam.nospam>
| Subject: Persisting the currently selected row on a GridView
| Date: Thu, 19 Jan 2006 12:40:05 -0800
| Lines: 19
| Message-ID: <61**********************************@microsoft.co m>
| 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!TK2MSFT NGXA03.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.
|

Jan 20 '06 #3

P: n/a
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==?=" <Ri***********@nospam.nospam>
| References: <61**********************************@microsoft.co m>
<c8**************@TK2MSFTNGXA02.phx.gbl>
| Subject: RE: Persisting the currently selected row on a GridView
| Date: Fri, 20 Jan 2006 01:10:04 -0800
| Lines: 112
| Message-ID: <F6**********************************@microsoft.co m>
| 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!TK2MSFT NGXA03.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==?=" <Ri***********@nospam.nospam>
| > | Subject: Persisting the currently selected row on a GridView
| > | Date: Thu, 19 Jan 2006 12:40:05 -0800
| > | Lines: 19
| > | Message-ID: <61**********************************@microsoft.co m>
| > | 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!TK2MSFT NGXA03.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.
| > |
| >
| >
|

Jan 23 '06 #4

P: n/a
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==?=" <Ri***********@nospam.nospam>
| References: <61**********************************@microsoft.co m>
<c8**************@TK2MSFTNGXA02.phx.gbl>
| Subject: RE: Persisting the currently selected row on a GridView
| Date: Fri, 20 Jan 2006 01:10:04 -0800
| Lines: 112
| Message-ID: <F6**********************************@microsoft.co m>
| 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!TK2MSFT NGXA03.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==?=" <Ri***********@nospam.nospam>
| > | Subject: Persisting the currently selected row on a GridView
| > | Date: Thu, 19 Jan 2006 12:40:05 -0800
| > | Lines: 19
| > | Message-ID: <61**********************************@microsoft.co m>
| > | 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!TK2MSFT NGXA03.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.
| > |
| >
| >
|

Jan 23 '06 #5

P: n/a
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==?=" <Ri***********@nospam.nospam>
| References: <61**********************************@microsoft.co m>
<c8**************@TK2MSFTNGXA02.phx.gbl>
<F6**********************************@microsoft.co m>
<Rc**************@TK2MSFTNGXA02.phx.gbl>
| Subject: RE: Persisting the currently selected row on a GridView
| Date: Mon, 23 Jan 2006 05:36:02 -0800
| Lines: 183
| Message-ID: <AF**********************************@microsoft.co m>
| 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==?=" <Ri***********@nospam.nospam>
| > | References: <61**********************************@microsoft.co m>
| > <c8**************@TK2MSFTNGXA02.phx.gbl>
| > | Subject: RE: Persisting the currently selected row on a GridView
| > | Date: Fri, 20 Jan 2006 01:10:04 -0800
| > | Lines: 112
| > | Message-ID: <F6**********************************@microsoft.co m>
| > | 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!TK2MSFT NGXA03.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==?=" <Ri***********@nospam.nospam>
| > | > | Subject: Persisting the currently selected row on a GridView
| > | > | Date: Thu, 19 Jan 2006 12:40:05 -0800
| > | > | Lines: 19
| > | > | Message-ID: <61**********************************@microsoft.co m>
| > | > | 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!TK2MSFT NGXA03.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.
| > | > |
| > | >
| > | >
| > |
| >
| >
|

Jan 23 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.