Solved

Sorting DBGrid

Posted on 1997-12-11
6
414 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

773 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