?
Solved

How To Sort a StringGrid

Posted on 1998-10-27
3
Medium Priority
?
219 Views
Last Modified: 2010-05-18
any ideas ?

0
Comment
Question by:k6__
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1344558
how big are the stringgrids?
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1344559
ps. why not using great freeware components?

look at supergrid and hypergrid :

http://www.pablop.demon.co.uk/

Zif.
0
 
LVL 8

Accepted Solution

by:
ZifNab earned 100 total points
ID: 1344560
Hi,

If you want to it yourself, then you can use TStringList's.

Here is some code (not by me, this one explains it already)

Put the columns into TStringLists, put the original order number of the ordered column in the object of that column string list, sort the ordered column, put the ordered column back in the stringgrid, put the unordered column items  against the ordered column items (original order in object of ordered column is                   index into un-ordered column TStringList).

    Like this :-

     var
      Col1StrLst, Col2StrLst : TStringList;
      i : integer;
     begin
      {put first column into the strings of a string list}
      Col1StrLst := TStringList.Create;
      with Col1StrLst do begin
       Assign(StringGrid1.Cols[1]);
      {put the original order number into the objects of the TStrings}
       for i := 0 to Count - 1 do
        Objects[i] := pointer(i); {note the type-cast}
      {now sort the strings - the objects (with references to the
       original position) stay with each string}
        Sorted := true;
       end;
    {put ordered strings back into first column}
       StringGrid1.Cols[1].Assign(Col1StrLst);
    {put second column into the strings of a string list}
       Col2StrLst := TStringList.Create;
       Col2StrLst.Assign(StringGrid1.Cols[2]);
    {put strings of second column in appropriate row for first column strings - the row number is in the object of the sorted first column}
       for i := 0 to Col0StrLst.Count - 1 do
        StringGrid1.Cells[2, i] :=
          Col2StrLst.Strings[integer(Col1StrLst.Objects[i])];        {note type-cast}
       {free the TStringLists}
       Col1StrLst.Free;
       Col2StrLst.Free;
      end;

The above assumes you have no headings, if you do you would have to save them to variables, set them to blank strings, then sort the rows, then set the heading strings back to the top row. The blank strings in the columns would always sort to the top.

Zif.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

762 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