Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 420
  • Last Modified:

Sorting DBGrid

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
MikeP090797
Asked:
MikeP090797
  • 2
  • 2
  • 2
1 Solution
 
ChrisLewisCommented:
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
 
ChrisLewisCommented:
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
 
MikeP090797Author Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mrmickCommented:
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
 
MikeP090797Author Commented:
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
 
mrmickCommented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now