Solved

How To Sort a StringGrid

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

0
Comment
Question by:k6__
  • 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 50 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now