From: SeePee on
How can I maintain the same selected row even after a sort on a DataGridView
with multiple pages?
I have been looking for an example for weeks and still no luck, so help
would be appreciated.

Thanks
From: Allen Chen [MSFT] on
Hi,


>How can I maintain the same selected row even after a sort on a
DataGridView
>with multiple pages?
>I have been looking for an example for weeks and still no luck, so help
>would be appreciated.

>Thanks

As far as I know there's no DataGridView control in ASP.NET. I assume you
mean GridView. You may refer to the following code. If you mean DataGrid
control the logic is similiar. The basic idea is to use PreRender event to
replace the entire row:

bool sorted = false;

protected void GridView1_PreRender(object sender, EventArgs e)
{
if (sorted)
{
var storedselectedrow = Session["selectedrow"] as
GridViewRow;
var replacedrow = Session["replacedrow"] as GridViewRow;
if (storedselectedrow != null)
{
Session["replacedrow"] = this.GridView1.SelectedRow;
int index = this.GridView1.SelectedIndex + 1;//+1
because of sorting header
if (index < this.GridView1.Controls[0].Controls.Count)
{
this.GridView1.Controls[0].Controls.RemoveAt(index);
this.GridView1.Controls[0].Controls.AddAt(index,
storedselectedrow);
}
}
}

}

protected void GridView1_Sorting(object sender,
GridViewSortEventArgs e)
{


var selectedrow= this.GridView1.SelectedRow;
if (selectedrow != null)
{
Session["selectedrow"] = selectedrow;

}
sorted = true;
}

Please let me know whether it works for you can feel free to ask if you
have further questions.

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg(a)microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.