Solved

Sorting DBGrid

Posted on 1997-12-11
6
418 Views
Last Modified: 2008-02-01
I need the records in a DBGrid to be sorted by a spicific field. I need that the records will reposition according to the sort order every time the user updates the field. How that can be done?
0
Comment
Question by:MikeP090797
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 3

Expert Comment

by:ChrisLewis
ID: 1444777
I think some one jsut asked this question, it might have even been you.

  Basically, if you are changing sort fields in the DBGrid, you have to refresh the query the DBGrid is based on to get the new sort order.  You cannot just "move" the row to the new position, you will actually have to re-retrieve your rows.  

  I know this is not what you want to here, but with DBgrid, you are limited to it's functionality
0
 
LVL 3

Expert Comment

by:ChrisLewis
ID: 1444778
Sorry, hit the submit button too early.

Anyway, continuing,

If you want to try, you might be able to do something with an unbound grid, and deal with it that way.  

Basically, you would retrieve your information, and load up a grid.

When the user selects a row, either pop a text box over the row for editing.  (note that you won't be editing the row directly)

you could then determine where in the grid to move the row to.  
Unfortunately, there is no INSERT command for a grid, so you'd have to move to the new row, move everything down 1 row, write the changed row in, move to the changed row, and move everything up one.

Doesn't sound nice.

Chris
0
 
LVL 8

Author Comment

by:MikeP090797
ID: 1444779
Re-retreiving the reocrds each time would be too slow. The RecordSource is a table. I tried to set an index to the table, but the first row disappears every time i add a row thta according to the sort order isn't the last one. What might be the reason for that?
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 8

Accepted Solution

by:
mrmick earned 400 total points
ID: 1444780
You can simulate the Access-like behavior of clicking on a column header to sort the records in the grid on the field whose column header is clicked by constructing an appropriate SQL statement using the "ORDER BY" clause and insert the name of the desired field on which the sort will be done. An example is below.

Private SubDBGrid1_HeadClick (ColIndex As Integer)
   Dim SQLStr As String

   'Build new SQL query based on column header clicked on
   SQLStr = "SELECT * FROM Authors ORDER BY "
   SQLStr = SQLStr & TDBGrid1.Columns(ColIndex).DataField
   Data1.RecordSource = SQLStr
   Data1.Refresh
End Sub

0
 
LVL 8

Author Comment

by:MikeP090797
ID: 1444781
This is not what i need. I need the records to be sorted every time the user updates the field that the table is sorted now. Is there anyway of doing that without reloading the whole table again?
0
 
LVL 8

Expert Comment

by:mrmick
ID: 1444782
Mike, the dbgrid doesn't have a method that automatically performs this for you.

DBGrid was written by APEX for Microsoft.  As a service to DBGrid users, APEX provides a free on-line help file that contains a wealth of detailed information not found in the Visual Basic documentation, with accompanying tutorial projects. This complete reference is the definitive source for information on DBGrid, and is a "must-have" for any DBGrid user. To download a copy, visit the APEX web site at http://www.apexsc.com.

I believe you can download the True DBGrid upgrade for free.

0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

631 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