Solved

DataGridView Sorting

Posted on 2006-07-20
11
484 Views
Last Modified: 2008-02-20
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
Comment
Question by:PhilippeRenaud
  • 7
  • 4
11 Comments
 
LVL 34

Expert Comment

by:Sancler
ID: 17145730
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
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 17145744
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
 
LVL 34

Accepted Solution

by:
Sancler earned 500 total points
ID: 17146539
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
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 17146664
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
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 17146746
For the 6 seconds...Its a timer
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 17146819
Im sorry, it returns a dataSet of the SELECT statement. My mistake
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17146833
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
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 17147033
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
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 17147106
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
 
LVL 34

Expert Comment

by:Sancler
ID: 17147136
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
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 17148139
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now