Home All Groups Group Topic Archive Search About

Updating SelectedIndex after delete

Author
16 Nov 2007 4:40 PM
Nirmal Singh
I have a delete button on my page which deletes the selected row in a
gridview.

After the deletion the selected index gets reset to -1.  How can I keep the
same selected index?

Nirmal

Author
18 Nov 2007 8:27 AM
Teemu Keiski
If the index of the deleted row is greater than the selectedindex, you could
take it into a local variable before deleting (perhaps in OnRowDeleting
event), and after delete operation (in OnRowDeleted event), set it back. If
the index of the deleted row is less than or equal to selected index, you'd
need to decrement one from the index (since it's not correct anymore as it
vontains the deleted row)-->could lead to index being out of range


--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net


Show quoteHide quote
"Nirmal Singh" <nirmal.si***@notthisbittowerhamlets.gov.uk> wrote in message
news:OYUlj9GKIHA.1204@TK2MSFTNGP03.phx.gbl...
>I have a delete button on my page which deletes the selected row in a
>gridview.
>
> After the deletion the selected index gets reset to -1.  How can I keep
> the same selected index?
>
> Nirmal
>
Author
19 Nov 2007 12:00 PM
Nirmal Singh
Teemu

I've added the following code in the OnRowDeleting event
    DeletedRow = e.RowIndex

In the OnRowDeleted event I have the following code
    RowCount = RowCount - 1
     If DeletedRow = RowCount Then
        grdCurrentAbsence.SelectedIndex = DeletedRow - 1
     Else
        grdCurrentAbsence.SelectedIndex = DeletedRow
     End If

RowCount is a local varaiable holding the number of rows in the gridview.
This is still resetting the selectedIndex to -1.

What am I doing wrong?

Nirmal Singh

Show quoteHide quote
"Teemu Keiski" <jot***@aspalliance.com> wrote in message
news:eNBonzbKIHA.4688@TK2MSFTNGP06.phx.gbl...
> If the index of the deleted row is greater than the selectedindex, you
> could take it into a local variable before deleting (perhaps in
> OnRowDeleting event), and after delete operation (in OnRowDeleted event),
> set it back. If the index of the deleted row is less than or equal to
> selected index, you'd need to decrement one from the index (since it's not
> correct anymore as it vontains the deleted row)-->could lead to index
> being out of range
>
>
> --
> Teemu Keiski
> AspInsider, ASP.NET MVP
> http://blogs.aspadvice.com/joteke
> http://teemukeiski.net
>
>
> "Nirmal Singh" <nirmal.si***@notthisbittowerhamlets.gov.uk> wrote in
> message news:OYUlj9GKIHA.1204@TK2MSFTNGP03.phx.gbl...
>>I have a delete button on my page which deletes the selected row in a
>>gridview.
>>
>> After the deletion the selected index gets reset to -1.  How can I keep
>> the same selected index?
>>
>> Nirmal
>>
>
>
Author
19 Nov 2007 12:17 PM
Nirmal Singh
Got it.

The OnRowDeleting event should be for the gridview but the OnRowDeleted
event should be for the datasource.

It's working as expected now.

Nirmal

Show quoteHide quote
"Nirmal Singh" <nirmal.si***@notthisbittowerhamlets.gov.uk> wrote in message
news:eBnE9OqKIHA.2432@TK2MSFTNGP04.phx.gbl...
> Teemu
>
> I've added the following code in the OnRowDeleting event
>    DeletedRow = e.RowIndex
>
> In the OnRowDeleted event I have the following code
>    RowCount = RowCount - 1
>     If DeletedRow = RowCount Then
>        grdCurrentAbsence.SelectedIndex = DeletedRow - 1
>     Else
>        grdCurrentAbsence.SelectedIndex = DeletedRow
>     End If
>
> RowCount is a local varaiable holding the number of rows in the gridview.
> This is still resetting the selectedIndex to -1.
>
> What am I doing wrong?
>
> Nirmal Singh
>
> "Teemu Keiski" <jot***@aspalliance.com> wrote in message
> news:eNBonzbKIHA.4688@TK2MSFTNGP06.phx.gbl...
>> If the index of the deleted row is greater than the selectedindex, you
>> could take it into a local variable before deleting (perhaps in
>> OnRowDeleting event), and after delete operation (in OnRowDeleted event),
>> set it back. If the index of the deleted row is less than or equal to
>> selected index, you'd need to decrement one from the index (since it's
>> not correct anymore as it vontains the deleted row)-->could lead to index
>> being out of range
>>
>>
>> --
>> Teemu Keiski
>> AspInsider, ASP.NET MVP
>> http://blogs.aspadvice.com/joteke
>> http://teemukeiski.net
>>
>>
>> "Nirmal Singh" <nirmal.si***@notthisbittowerhamlets.gov.uk> wrote in
>> message news:OYUlj9GKIHA.1204@TK2MSFTNGP03.phx.gbl...
>>>I have a delete button on my page which deletes the selected row in a
>>>gridview.
>>>
>>> After the deletion the selected index gets reset to -1.  How can I keep
>>> the same selected index?
>>>
>>> Nirmal
>>>
>>
>>
>
>