Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting in grid

Status
Not open for further replies.

earlrainer

Programmer
Mar 1, 2002
170
IN
Hi guys,
I dont think this is possible..but anyways lets try...

I have a dbgrid on my form.The dataset of the grid's datasource is a query.
what i want to do is, when the user clicks a column heading , then the grid should be sorted by that column.

I had read some posts about achieving the above if the dataset is a Table,but not if it is a query.
Also even if the dataset is a Table, then i can sort only if the column is indexed(or something like that)

Can you guys help me to do the same with a query and also be able to sort on non indexed columns..

it would be really great if you guys help me on this one

 
hi

I had the same problem and we had to buy a third party component to do the job. It behaves like Outlook or File explorer when sorting. If you're interested, the component is by TMSSoftware ( and is called TDBAdvStringGrid.

I highly recommend it and it's not expensive.

lou
[penguin]
 
First in the IndexFieldnames of the TQuery or TTable fill some fields in, for multiple fields separate them by ;

Select the on TitleClick event of the DBGrid and fill in:

procedure TMyForm.dbGrid1TitleClick(Column: TColumn);
begin
myDtaMod.Mytable.IndexFieldNames := Column.FieldName;
end;

-----------------------------------------------------
Another feature is to build in a search.

Put an editbox on the form and:

procedure TMyForm.Edit1Change(Sender: TObject);
begin
if (edtSearch.Text <> '') then
try
myDtaMod.Mytable.FindNearest([edit1.Text]);
except
on EDatabaseError do;
on EconvertError do;
end;
end;

With this a search will be done in the active column

Regards S. van Els
SAvanEls@cq-link.sr
 
hi,

If you have a query, you could use the orderby property. The only thing you do is change the ordeby of the result set. It automaticaly sorts the grid.

Steph
 
Hi Guys,

Svansel,your solution works great for a Ttable,but does not work quite as well for a Tquery because Tquery does not have a indexname property..how do i go about this.

Svanhooft,I think your solution would require to execute the query once again..and I want to avoid this at all costs simply because the query takes ages to return the result..

..hope you guys help me on this one
 
I see you are right, I am using this actually with tables in 1 of my projects, where use also a lot of queries.

What type of database are you using?

For Ado use a TADODataset

Else use the ClientDataSet or the BDEClientDataSet, it will work according to my sources.
I didn't intercepted it because I don't use ClientDataSets (yet) and it is a feature I use only in certain tables for administrative purpose. S. van Els
SAvanEls@cq-link.sr
 
hi,

you are using a Tquery component i think. So you use this via the BDE. If you have the posibility upgrade to interbase or Mysql. If you use Interbase, you can limited the amount of records in your result set. So it will run very fast. Also changing to an other order will be done in a millisecond.
If you stuck with the Tquery, It will always return the whole result set and every time you change the order it will take the same amount of time. The other posibility is to use a TTable and index all the fields in the grid.

alternative method.
Donn't use a dbgrid but download virtualtreeview. This is a complete different way to solve your problem. It is not a simpel solution, but one which has great opportunities.
We use it ourselfs in every project we build. The VT is a component completly for free. The approach is that you use your query and put the result set via a datarecord in that tree. So al your data is loaded in memory. Via the enormous amount of properties and eventhandlers you can do every thing with your data. Sort on every column, speedsearching, coloring, every thing you would like to do is possible. It took us a day to get every thing working, and now every day we discover some new things. There is great support group which is very active. You can download this component at


Give it a try, look beyond the difficulties, it is a great component.
Example it took 5-6 seconds to fill the tree with 650.000 records.

Steph.
 
hi,

you are using a Tquery component i think. So you use this via the BDE. If you have the posibility upgrade to interbase or Mysql. If you use Interbase, you can limited the amount of records in your result set. So it will run very fast. Also changing to an other order will be done in a millisecond.
If you stuck with the Tquery, It will always return the whole result set and every time you change the order it will take the same amount of time. The other posibility is to use a TTable and index all the fields in the grid.

alternative method.
Donn't use a dbgrid but download virtualtreeview. This is a complete different way to solve your problem. It is not a simpel solution, but one which has great opportunities.
We use it ourselfs in every project we build. The VT is a component completly for free. The approach is that you use your query and put the result set via a datarecord in that tree. So al your data is loaded in memory. Via the enormous amount of properties and eventhandlers you can do every thing with your data. Sort on every column, speedsearching, coloring, every thing you would like to do is possible. It took us a day to get every thing working, and now every day we discover some new things. There is great support group which is very active. You can download this component at


Give it a try, look beyond the difficulties, it is a great component.
Example it took 5-6 seconds to fill the tree with 650.000 records.

Steph.
 
Hi,

Thanks guys for all yor help.
I will certainly try the treeview component.

But i must admit i am a trifle bit disapppinted that it could not be done with a Tquery.

anyway..Borland here is a wish list............
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top