Maintaining the selected row after sorting an ASP.NET DataGridView

Discussion in 'ASP .Net' started by SeePee, Jul 20, 2010.

  1. SeePee

    SeePee Guest

    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
    SeePee, Jul 20, 2010
    #1
    1. Advertising

  2. 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:
    .

    ==================================================
    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.
    Allen Chen [MSFT], Jul 21, 2010
    #2
    1. Advertising

  3. SeePee

    SeePee Guest

    RE: Maintaining the selected row after sorting an ASP.NET DataGrid

    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:
    > .
    >
    > ==================================================
    > 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.
    >
    > .
    >
    SeePee, Jul 22, 2010
    #3
  4. RE: Maintaining the selected row after sorting an ASP.NET DataGrid

    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:
    .
    Allen Chen [MSFT], Jul 23, 2010
    #4
  5. SeePee

    SeePee Guest

    RE: Maintaining the selected row after sorting an ASP.NET DataGrid

    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:
    > .
    >
    > .
    >
    SeePee, Jul 23, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hangar18
    Replies:
    0
    Views:
    10,342
    hangar18
    Mar 27, 2006
  2. =?Utf-8?B?UCBL?=

    DataGridView - Determine if a row has been edited

    =?Utf-8?B?UCBL?=, Apr 20, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    868
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    Apr 20, 2006
  3. Replies:
    4
    Views:
    917
  4. D
    Replies:
    0
    Views:
    198
  5. Matthew Humphrey

    Disable datagridview sorting

    Matthew Humphrey, Oct 9, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    732
Loading...

Share This Page