Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Sorting Dataset

Posted on 2005-04-28
6
Medium Priority
?
485 Views
Last Modified: 2010-04-05
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!
0
Comment
Question by:hibbidiji
  • 4
6 Comments
 
LVL 17

Accepted Solution

by:
geobul earned 2000 total points
ID: 13892518
Hi,

You may use IndexFieldNames property of the ClientDataset. Set its value to the field name in column header click event.

Alternatively you may create an index to your client dataset in the header click event of the grid. See TClientDataSet.AddIndex method. You may use the same index name and you'll have to delete the index before adding a new one. Apply the newly added index by setting IndexName property.

Regards, Geo
0
 
LVL 1

Author Comment

by:hibbidiji
ID: 13899871
How would I specify ascending or descending sort?
0
 
LVL 1

Author Comment

by:hibbidiji
ID: 13924704
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!



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!

 
LVL 1

Author Comment

by:hibbidiji
ID: 13965715
Anyone?
0
 
LVL 11

Expert Comment

by:pcsentinel
ID: 13975595
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
0
 
LVL 1

Author Comment

by:hibbidiji
ID: 13994306
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
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Screencast - Getting to Know the Pipeline
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question