Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 496
  • Last Modified:

DataGridView Sorting

Hi,

I got a datagridview and I got also a Timer. Each 6 seconds, it refresh the grid to update it from SQL.
My problem is, on refresh, if just before I click on the header to sort it, at the refresh, it will get the datasource fill again then show (thats the refresh) so the data will come back sorted as how it was the first time onLoad because it dont remember that someone could click on sort of any column

I would like that the refresh could refresh the data of course, but keep the sort in mind...any ideas ?
0
PhilippeRenaud
Asked:
PhilippeRenaud
  • 7
  • 4
1 Solution
 
SanclerCommented:
How are you refreshing the datagridview?  If you leave its bindings exactly as they are and just refresh the datasource then the new data should appear in the datagridview maintaining its (the datagridview's) current sortorder.

Roger
0
 
PhilippeRenaudAuthor Commented:
oh okai, I was doing this:  fillRooms()  

Private Sub fillRooms()
        dtRooms = oData.selectTable.Tables(0)
        dgRooms.DataSource = dtRooms
        dgRooms.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells)
        dgRooms.Columns(0).Visible = False
End Sub

How could I just refresh the datasource ... dgRooms.refresh?    or dgRooms.dataSource.refresh something like that
0
 
SanclerCommented:
When, which you only need to do once, you have set your datagrid's datasource (and other properties) with the code that you show, it should show the data which exists in the datasource at any given time.  This assumes that that datasource continues to exist.  You say you "refresh the grid to update it from SQL".  What that actually means, I think, is that you refill the datatable with fresh data.  If you are doing it by destroying the datatable and making a new one every time then the assumption that "that datasource continues to exist" is not true, and you will have to continue with your present approach - of resetting the datasource every time, and that will lose its existing sort order.  But if you are emptying, rather than destroying, the datatable and then putting the fresh data in it, the datagrid's bindings and all its other settings will remain intact but it will show the new, rather than the old, data.

So how are you handling the updating of the datatable (not the datagrid) "Each 6 seconds"?

Roger
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
PhilippeRenaudAuthor Commented:
I understand what you mean by "emptying rather than destroying"
Does that mean when we write dgRooms.DataSource = dtRooms, it equals to "destroying" ?
What could be "emptying" then ?

well my data table is update by oData.selectTable.Tables(0). This actually goes in a Public Function in a class wich in it as a SQLstring. it returns a dataTable of the SELECT statement.
0
 
PhilippeRenaudAuthor Commented:
For the 6 seconds...Its a timer
0
 
PhilippeRenaudAuthor Commented:
Im sorry, it returns a dataSet of the SELECT statement. My mistake
0
 
SanclerCommented:
Consider this as an outline

     Dim conn As New Connection("MyConnectionString")
     Dim da As New DataAdapter("SELECT * FROM MyTable", conn)

Now there are two alternative scenarios.  One is to have one procedure which constantly makes a new datatable when it wants the data to be refreshed

     Dim dtRooms As New DataTable
     da.Fill(dtRooms)

That is what I meant by "destroying" the existing table.  It is, as an object, replaced with a different new object every time.  The other is to have one object

     Dim dtRooms As New DataTable

and then keep refilling it when you want to refresh the data

     dtRooms.Clear()
     da.Fill(dtRooms)

That is what I meant by "emptying".

I know that you have your table in a dataset, so it might look different from this.  But I am trying to illustrate the principle, as simply as possible.

Does that explain the point?  Please don't hesitate to say if it doesn't.

Roger
   
0
 
PhilippeRenaudAuthor Commented:
You know what, I understand everything.

the only thing is, Im lost on where this will help when I need the refresh or update the datagrid itself!
Fine, lets say i do dtrooms.clear()
Then i fill it again...

Datagrid is filled at the OnLoad.
So if I want to see the new "datagrid updated", I need to refresh it or something like that no?. This is that refresh I fear it will unsort..

Am I wrong ?
0
 
PhilippeRenaudAuthor Commented:
Lol Sancler, look at this, I found it. You helped me thanks. It is so sweet:

    Private Sub Timer3_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer3.Tick
        dtRooms.AcceptChanges()
    End Sub

That's it. Its working. Each 6 seconds, DataTable commits changes.
0
 
SanclerCommented:
I hesitate to say you are wrong ;-)  But on what I know of your program I think you might be pleasantly surprised if you try it.  The whole point of databinding is that YOU don't need to "refresh or update the datagrid".  The databinding does that.  Every time the data in the datasource changes the binding reflects those changes in the datagrid.  It's not long since I read a question which was complaining that, when a new record was added to a bound and sorted datagrid it would insist on going to its proper place in the sorted order rather than sticking at the end where it was added.

Try it.

If it doesn't do what you want, we'll have a look why.

Roger
0
 
PhilippeRenaudAuthor Commented:
Alright I will do some testing and I will come back to you with what is going on with that.
If there is a problem, i'll a Part 2 on my Question with a 500 points for you lol.


Have a nice day. I'm off!
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.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now