From: SeePee on
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 [MSFT]" wrote:

> 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.
>
> .
>
From: Allen Chen [MSFT] on
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:
msdnmg(a)microsoft.com.

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

Chris

"Allen Chen [MSFT]" wrote:

> 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:
> msdnmg(a)microsoft.com.
>
> .
>