Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Maintaining the selected row after sorting an ASP.NET DataGridView

Reply
Thread Tools

Maintaining the selected row after sorting an ASP.NET DataGridView

 
 
SeePee
Guest
Posts: n/a
 
      07-20-2010
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
 
Reply With Quote
 
 
 
 
Allen Chen [MSFT]
Guest
Posts: n/a
 
      07-21-2010
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/subs...#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/subs.../aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
 
 
 
SeePee
Guest
Posts: n/a
 
      07-22-2010
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/subs...#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/subs.../aa948874.aspx
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
> .
>

 
Reply With Quote
 
Allen Chen [MSFT]
Guest
Posts: n/a
 
      07-23-2010
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:
.

 
Reply With Quote
 
SeePee
Guest
Posts: n/a
 
      07-23-2010
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:
> .
>
> .
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C++ 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C Programming 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui Python 0 04-27-2009 12:46 PM
Disable datagridview sorting Matthew Humphrey ASP .Net Datagrid Control 3 12-07-2006 02:50 PM
ok I can do a totals row but how about a percentage row after each data row D ASP .Net Datagrid Control 0 05-23-2005 04:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57