Board index » delphi » Sort Table

Sort Table

Want to sort table by different columns, could use a query and add ORDER
BY clause, but this seems to be quite aomplicated way.
Is there any table1.sortby clause that I haven't found or can this be
done by the displaying GB-Grid component?
thanks for help
 

Re:Sort Table


Quote
Stefan Tauscher wrote:

> Want to sort table by different columns, could use a query and add ORDER
> BY clause, but this seems to be quite aomplicated way.
> Is there any table1.sortby clause that I haven't found or can this be
> done by the displaying GB-Grid component?
> thanks for help

I dont know what they've done in the newer versions, but in 2.0
I would try the following.  The easy way if you dont need the user to
be able to choose which fields are sorted dynamically is to just make
a couple secondary indexes with the database desktop.  Then set the
indexname
property like this Table1.IndexName := 'CustNoIndex';
You could put the index names in a combobox dropdown list.

If you want to do it the sql way using the orderby clause I will show
you how I
might approach it.

procedure TForm2.ComboBox2DropDown(Sender: TObject);
var
FieldNames: TStringList;
i:          integer;

begin

IF dbgrid1.fieldcount=0 then
raise exception.create('please run report'+chr(13)+
'no fields available');

  combobox2.items.clear;
 FieldNames := TStringList.Create;
{ Get the names }
  Query1.GetFieldNames(FieldNames);

  fieldnames.count;

  for i:= 1 to fieldnames.count do
   begin
combobox2.items.add(fieldnames.strings[i-1]);
  end;
    fieldnames.free;
end;

This fills a combobox with fieldnames.  You can then
dynamically set the orderby clause referencing the combobox2.text
when you type the query1.sql.add('order by  '+combobox2.text)

Looking @ the above code I see a weakness.  Maybe someone could help
improve upon it.   What if you wanted to sort by three fields and needed
three
or more comboboxes.   You could use this method in the in the ondropdown
event
for all your comboboxes but it seems kind of redundant.  I didnt see
where I could
get the combobox name at run time with one line of code.

I think you could rewrite it kind of like this and make one procedure.

procedure tform1.getnames;

var
  I: Integer;
FieldNames: TStringList;
A:          integer;

begin

IF dbgrid1.fieldcount=0 then  (* where the dbgrid datasource is the
result of a query*)
raise exception.create(''no fields available');

  for A := 0 to ComponentCount -1 do
     if Components[A] is TCombobox then
       if (Tcombobox(Components[A]).Name = 'Combobox1')or
        (Tcombobox(Components[A]).Name = 'Combobox2') or
  (Tcombobox(Components[A]).Name = 'Combobox3') then
begin

  Tcombobox(Components[A]).items.clear;
 FieldNames := TStringList.Create;
{ Get the names }
  Query1.GetFieldNames(FieldNames);

  fieldnames.count;

  for i:= 1 to fieldnames.count do
   begin
Tcombobox(Components[A]).items.add(fieldnames.strings[i-1]);
  end;
    fieldnames.free;
end;
end;

Now you'll notice that I'm still not done.  If you had three
combobox calling this same method the user could select
custno custno custno in all three combobox which really
isnt correct.  I think you can get around this by putting a few lines of
code in the changevalue method of each combobox .  Raise an
exception that says field already selected.

Maybe someone has some quicker more efficient code they will
share with you. I hope this helps if they dont.

Later
jim

Re:Sort Table


Quote
Jim McNamara wrote:

> Stefan Tauscher wrote:

> > Want to sort table by different columns, could use a query and add ORDER
> > BY clause, but this seems to be quite aomplicated way.
> > Is there any table1.sortby clause that I haven't found or can this be
> > done by the displaying GB-Grid component?
> > thanks for help

sorry.  I pushed the button to send the initial message and
realized I had an error.  I checked this code out by placing
components on the form and ran the code.  This code seems to work
for the getnames procedure now.  You could probably run getnames
from a button click event.

procedure TForm1.Button1Click(Sender: TObject);
begin
getnames;
end;

Quote

> procedure tform1.getnames;

var
  I: Integer;
FieldNames: TStringList;
A:          integer;
s:          string;

begin

IF dbgrid1.fieldcount=0 then  (* where the dbgrid datasource is the
result of a query*)
raise exception.create('no fields available');

  for A := 0 to ComponentCount -1 do
  begin

     if Components[A] is TCombobox then
     begin
     s:=form1.components[a].name;
     if (comparetext(s,'Combobox1')=0) or
     (comparetext(s,'Combobox2')=0)or
     (comparetext(s,'Combobox3')=0) then

     {showmessage(s);}

  tcombobox(components[a]).items.clear;
 FieldNames := TStringList.Create;
{ Get the names }
  Query1.GetFieldNames(FieldNames);
  fieldnames.count;
  for i:= 1 to fieldnames.count do
   begin
 tcombobox(components[a]).items.add(fieldnames.strings[i-1]);
  end;

fieldnames.free;

end;
 end;
  end;

Quote
> Later
> jim

Re:Sort Table


Stefan,

this is very bad practice. use TQuery

Best regards,
Emil Beli                        

http://www.greenhousemm.com
ICQ: 1878554

Monday, 06 November, 2000, 15:56:09 PM, you wrote:

ST> Want to sort table by different columns, could use a query and add ORDER
ST> BY clause, but this seems to be quite aomplicated way.
ST> Is there any table1.sortby clause that I haven't found or can this be
ST> done by the displaying GB-Grid component?
ST> thanks for help

Other Threads