?
Solved

How to keep sort state on datagrid after refresh

Posted on 2006-06-07
10
Medium Priority
?
676 Views
Last Modified: 2012-06-21
Hi Experts!

I am using a Windows Forms datagrid, that shows data that is brought  from a remote Web Service in xml, then converted to a dataset. Once I have the dataset, I use one of the tables as my datagrid's data source.

The process of bringing the data occurs every 5 seconds, so I fetch the data and refresh the datagrid every 5 seconds using a timer to get the data displayed.

This is what happens every 5 seconds:

1- Get xml from web service
2- Convert xml into dataset
3- Use one of the dataset's tables as the datagrid datasource (DataGrid1.DataSource = dataSet1.Tables(0)
4- Refresh the datagrid (DataGrid1.refresh)

The problem is that I need the current sorting to be kept even after the new data is feteched and the data grid is refreshed. I also lose the selected row. Is there a way to keep the data sorted after refreshing the grid? Keeping the current selected row would be very useful too.

Thanks!!

glopezz
0
Comment
Question by:glopezz
  • 5
  • 2
7 Comments
 
LVL 6

Expert Comment

by:willbdman
ID: 16859492
I have an idea, but firstly how are you sorting your datagrid?
0
 
LVL 6

Accepted Solution

by:
willbdman earned 750 total points
ID: 16859575
Heres a way of restoring a selected row using the selected index of the datagrid, howvere this may not be the best option (what happens if data is added above where you are selected? - you have the row above the one you previously selected.

#Region "Global Variables"

    ' Stores Selected row
    Private intRow As Integer = 0

#End Region

    ' Run before refresh
    Private Sub SaveGrid()

        ' Stores currently selected row
        Me.intRow = Me.DataGrid1.CurrentRowIndex

    End Sub

    ' Run after refresh
    Private Sub LoadGrid()

        ' Stores currently selected row
        Me.intRow = Me.DataGrid1.CurrentRowIndex

    End Sub

Alternatively look here to see a way of logging the currently selected row:-

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21484951.html?query=sorting+datagrid+vb.net&clearTAFilter=true

0
 

Author Comment

by:glopezz
ID: 16864799
Thanks willbdman, I am testing this right now. How about the current sort? Is it possible to maintain it?

Thanks
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:glopezz
ID: 16864880
OK, saving the row sub works fine, however although the small "arrow" of the selected index is positioned where it shoud, the entire row is not "highlighted" as it was originally. Is there a way to highlight it?

Thanks
0
 
LVL 6

Expert Comment

by:willbdman
ID: 16869435
I'm not sure with that one....

only thin i could think of would be to change the selection background colour:-

Me.DataGrid1.SelectionBackColor = Color.Blue

however, this would cause problems with other selected rows.
0
 
LVL 6

Expert Comment

by:willbdman
ID: 17059161
What about the answer I gave?
0
 
LVL 6

Expert Comment

by:willbdman
ID: 17059167
He uses the first answer I Gave
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question