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!
I will be given a column name and will want to sort based on it.
HELP!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.DBGridView1SortColu mn(Sender: TObject; ACol: Integer; Ascending: Boolean);
begin
clientdataset1.close;
clientdataset1.IndexFieldN ames :='';
if Ascending then
begin
ClientDataset1.IndexFieldN ames := DBGridView1.Columns[ACol]. FieldName;
end;
if not Ascending then
begin
clientdataset1.IndexDefs.A ddIndexDef .Options := [ixDescending];
clientDataset1.IndexFieldN ames := 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!
procedure TForm1.DBGridView1SortColu
begin
clientdataset1.close;
clientdataset1.IndexFieldN
if Ascending then
begin
ClientDataset1.IndexFieldN
end;
if not Ascending then
begin
clientdataset1.IndexDefs.A
clientDataset1.IndexFieldN
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!
ASKER
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
regards
ASKER
procedure TForm1.DBGridView1SortColu mn(Sender: TObject; ACol: Integer; Ascending: Boolean);
begin
if dbsortable = true then begin
clientdataset1.IndexFieldN ames :='';
if Ascending then
begin
ClientDataset1.AddIndex(DB GridView1. Columns[AC ol].FieldN ame+'_asc' , DBGridView1.Columns[ACol]. FieldName, []);
clientdataset1.IndexName := DBGridView1.Columns[ACol]. FieldName+ '_asc';
end;
if not Ascending then
begin
ClientDataset1.AddIndex(DB GridView1. Columns[AC ol].FieldN ame+'_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
begin
if dbsortable = true then begin
clientdataset1.IndexFieldN
if Ascending then
begin
ClientDataset1.AddIndex(DB
clientdataset1.IndexName := DBGridView1.Columns[ACol].
end;
if not Ascending then
begin
ClientDataset1.AddIndex(DB
clientdataset1.IndexName := DBGridView1.Columns[ACol].
end;
end;
end;
Was the final code used to achieve my result
ASKER