Solved

DataGridView Sorting

Posted on 2006-07-20
11
483 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
For the 6 seconds...Its a timer
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Author Comment

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

Expert Comment

by:Sancler
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

11 Experts available now in Live!

Get 1:1 Help Now