0
votes

I have a Gridview where I have functionality of Update row. Upon clicking update row a text box is generated in the row where user can update the record for the particular row. However, if the user selects row 2 of gridview( page=1)for update and then clicks on say page 4 without saving. Page 4 is now displayed and row 2 is enabled for update on page 4. Moreover, if the user selects row 2 of gridview (page=1) then clicks on say page 4. Page 4 is now displayed and row 2 is highlighted, so to the User it looks like they have row2 of page 4 selected, I need to have the selectedrow move with the paging to not confuse users. Below is my Gridview defination:

<asp:GridView ID="grdDisountList" runat="server" PersistedSelection="false" CellPadding="4" ForeColor="#333333" GridLines="None"
        AutoGenerateColumns="false" Width="100%" OnRowEditing="EditRow" OnRowCancelingEdit="CancelEditRow"
        OnRowUpdating="UpdateRow" DataKeyNames="Discount_Id" AllowPaging="true" OnRowDataBound="grdDisountList_RowDataBound"
        PageSize="15" OnPageIndexChanging="OnPageIndexChanging" OnRowCommand="SelectRow" OnSelectedIndexChanged="grdDisountList_SelectedIndexChanged">

Any Ideas?

1

1 Answers

1
votes

The selected row only works on the visible items in the GridView. So when you change a page the selected item will still be the 2nd selected row, even though it is a completely different item.

The best option is to set the selected row to -1 in the OnPageIndexChanging event.

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.SelectedIndex = -1;
    GridView1.EditIndex = -1;
}

Os if you really want to keep the SelectedIndex, save it in a Session or ViewState along with the PageIndex and re-select the correct one based on those values.