protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            detailview1.PageIndex = GridView1.SelectIndexl;

        }

this show gridview first page data when i go to second page of griedviw detail view show dat which is in first page of griedview not a current dta why?

Dani AI

Generated

The behavior happens because GridView.SelectedIndex is the zero-based row index on the current page, while DetailsView.PageIndex is the zero-based record index in the whole data source. On page 2, selecting the first visible row yields SelectedIndex = 0, so assigning that to DetailsView.PageIndex shows the first record of the entire set (page 1). This explains the symptom reported by and why and pointed toward index semantics and rebinding.

A simple fix is to compute the absolute record index before setting the DetailsView page, then refresh the DetailsView:

int absoluteIndex = (GridView1.PageIndex * GridView1.PageSize) + GridView1.SelectedIndex;
DetailsView1.PageIndex = absoluteIndex;
DetailsView1.DataBind();

A more robust (recommended) pattern is to use the primary key: set GridView.DataKeyNames, read GridView.SelectedDataKey.Value in SelectedIndexChanged, and pass that key as a parameter to the DetailsView/SqlDataSource select query. That avoids index math and works reliably when sorting or filtering changes row order.

Troubleshooting checklist: confirm GridView.PageSize is correct, ensure the Select button raises SelectedIndexChanged, set DataKeyNames on the GridView, and fix typos (e.g., use SelectedIndex, not misspellings). If binding manually, always rebind the DetailsView after changing PageIndex or its select parameter.

Recommended Answers

All 3 Replies

Hi,

GridView.SelectedIndexChanged Event occurs when a row's Select button is clicked, but after the GridView control handles the select operation. GridView.SelectedIndex property returns the zero-based index of the selected row in a GridView control. You are trying to assign the row index to the PageIndex property which is completely wrong.

After setting up PageIndex, please rebind the detailsview

In case, if you are using SqlDatasource then

detailview1.PageIndex = GridView1.SelectIndexl;
defailview1.DataBind();

Hi Kiran,

Ignore my previous post. I though you assigned the GridView.SelectedIndex to GridView.PageIndex. I didn't see detailview1.PageIndex properly.

Sorry for the inconvenience. Please follow the adatapost reply.

commented: Hey pal! No need to say sorry :) OP is unclear. +12
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.