How to keep sort state on datagrid after refresh

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
glopezzAsked:
Who is Participating?
 
willbdmanCommented:
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
 
willbdmanCommented:
I have an idea, but firstly how are you sorting your datagrid?
0
 
glopezzAuthor Commented:
Thanks willbdman, I am testing this right now. How about the current sort? Is it possible to maintain it?

Thanks
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
glopezzAuthor Commented:
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
 
willbdmanCommented:
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
 
willbdmanCommented:
What about the answer I gave?
0
 
willbdmanCommented:
He uses the first answer I Gave
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.