Maintaining the selected row after sorting an ASP.NET DataGridView

S

SeePee

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
 
A

Allen Chen [MSFT]

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.

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:
(e-mail address removed).

==================================================
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.
 
S

SeePee

Sorry, yes it is a GridView which I have in Default.aspx.
Is it possible if you could supply the code in VB?

Thanks
Chris

Allen Chen said:
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.

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:
(e-mail address removed).

==================================================
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.

.
 
A

Allen Chen [MSFT]

Hi Chris,
Sorry, yes it is a GridView which I have in Default.aspx.
Is it possible if you could supply the code in VB?
Thanks
Chris

Please refer to the following code:

Dim sorted As Boolean = False

Protected Sub GridView1_PreRender(ByVal sender As Object, ByVal e As
EventArgs)

If sorted Then
Dim storedselectedrow = Session("selectedrow")
Dim replacedrow = Session("replacedrow")
If Not storedselectedrow Is Nothing Then

Session("replacedrow") = Me.GridView1.SelectedRow
Dim index = Me.GridView1.SelectedIndex + 1 '+1 because of
sorting header
If index < Me.GridView1.Controls(0).Controls.Count Then

Me.GridView1.Controls(0).Controls.RemoveAt(index)
Me.GridView1.Controls(0).Controls.AddAt(index,
storedselectedrow)
End If

End If

End If

End Sub

Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As
GridViewSortEventArgs)



Dim selectedrow = Me.GridView1.SelectedRow
If Not selectedrow Is Nothing Then

Session("selectedrow") = selectedrow

End If

sorted = True
End Sub

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:
(e-mail address removed).
 
S

SeePee

Allen, this works fine. I have been trying to find a solution for weeks now.
Many Thanks

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top