Solved

Sorting DBGrid

Posted on 1997-12-11
6
412 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
  • 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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