Link to home
Start Free TrialLog in
Avatar of hibbidiji
hibbidiji

asked on

Sorting Dataset

I am using an xml transform to pull data into my dataset.  This is then put intoa dbgridview.   The dbgrid I'm using (berg) has events for header clicking and automatically puts the asc or desc arrow in the header.   When using a simple stringgrid, all the sorting is done automatically, but when using the dbgrid version, I need to sort the data myself.   I dont know how I can do this with a clientdataset from an xmltransform.  

I will be given a column name and will want to sort based on it.

HELP!
ASKER CERTIFIED SOLUTION
Avatar of geobul
geobul

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hibbidiji
hibbidiji

ASKER

How would I specify ascending or descending sort?
Here is my procedure.   My dbgrid has a sort column feature that I'm using.    It will tell me which column has been clicked and if it is in ascending or descending sort at the moment.   Please help me correct my code.   as of the moment, it will only sort ascending. I'm obviously using incorrect syntax for the second case.

procedure TForm1.DBGridView1SortColumn(Sender: TObject; ACol: Integer; Ascending: Boolean);
begin
clientdataset1.close;
clientdataset1.IndexFieldNames :='';
 if Ascending then
begin
ClientDataset1.IndexFieldNames := DBGridView1.Columns[ACol].FieldName;
end;

if not Ascending then
begin
 clientdataset1.IndexDefs.AddIndexDef.Options := [ixDescending];
clientDataset1.IndexFieldNames := DBGridView1.Columns[ACol].FieldName;
end;

clientdataset1.Open;
end;

Also,
I have a numeric column in the dataset that has values like this:

45
100
23
250
500
350

for NOW if it sorts ascending, it will do thusly:

100
23
250
350
45
500

basing the sort solely on the first number.    I need it to sort in a real world way:
23
45
100
250
350
500

Thanks!



Anyone?
Ok having been pointed here by your other question I can at least see what the sorting problem is. The Number column is being sorted as text and not numeric. This means that the field is being seen as text, so if the originating field is numeric then your XML Transform must be converting it to string.

regards
procedure TForm1.DBGridView1SortColumn(Sender: TObject; ACol: Integer; Ascending: Boolean);

begin
if dbsortable = true then begin
clientdataset1.IndexFieldNames :='';
 if Ascending then
begin
    ClientDataset1.AddIndex(DBGridView1.Columns[ACol].FieldName+'_asc', DBGridView1.Columns[ACol].FieldName,[]);

clientdataset1.IndexName := DBGridView1.Columns[ACol].FieldName+'_asc';
end;

if not Ascending then
begin
       ClientDataset1.AddIndex(DBGridView1.Columns[ACol].FieldName+'_desc', DBGridView1.Columns[ACol].FieldName, [ixDescending]);
clientdataset1.IndexName := DBGridView1.Columns[ACol].FieldName+'_desc';
end;


end;
end;

Was the final code used to achieve my result