?
Solved

TStringGrid

Posted on 2000-04-10
4
Medium Priority
?
539 Views
Last Modified: 2010-05-18
Need help, how to sort Cols in TStringGrid.
0
Comment
Question by:danyb
[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
4 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 2703091
Try TAdvStringGrid from

http://www.tmssoftware.com/
0
 
LVL 1

Accepted Solution

by:
DValery earned 150 total points
ID: 2704575
Hi, danyb

Try this code:

------------------cut----------------
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    BitBtn1: TBitBtn;
    Memo1: TMemo;
    Edit1: TEdit;
    procedure BitBtn1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.BitBtn1Click(Sender: TObject);
var k,j,i: integer;
    ls: TStringList;
begin
  ls := TStringList.Create;
  k := StrToIntDef(Edit1.Text,0);
  try
   for i := 1 to StringGrid1.RowCount-1 do begin
     j :=ls.AddObject(StringGrid1.Rows[i].Strings[k],TStringList.Create);
     TStringList(ls.Objects[j]).AddStrings(StringGrid1.Rows[i]);
   end;
   ls.Sorted := True;
   for i := 1 to StringGrid1.RowCount-1 do
     StringGrid1.Rows[i] := TStringList(ls.Objects[i-1]); // Update Rows after Sort
  finally
   for i := 0 to ls.Count-1 do
       TStringList(ls.Objects[i]).Free;
   ls.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  with StringGrid1 do
  begin
    Cells[1,0] := 'Column 1';
    Cells[2,0] := 'Column 2';
    Cells[3,0] := 'Column 3';
    Cells[4,0] := 'Column 4';
    Cells[0,1] := 'Row 1';
    Cells[1,1] := 'Object';
    Cells[2,1] := 'Pascal';
    Cells[3,1] := 'is';
    Cells[4,1] := 'excellent';
    Cells[0,2] := 'Row 2';
    Cells[1,2] := 'Delphi';
    Cells[2,2] := 'is';
    Cells[4,2] := 'RAD';
  end;

end;

end.
------------------cut----------------
object Form1: TForm1
  Left = 173
  Top = 113
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object StringGrid1: TStringGrid
    Left = 56
    Top = 24
    Width = 521
    Height = 145
    RowCount = 3
    TabOrder = 0
  end
  object BitBtn1: TBitBtn
    Left = 64
    Top = 360
    Width = 75
    Height = 25
    Caption = 'BitBtn1'
    TabOrder = 1
    OnClick = BitBtn1Click
  end
  object Memo1: TMemo
    Left = 56
    Top = 184
    Width = 521
    Height = 137
    ScrollBars = ssVertical
    TabOrder = 2
  end
  object Edit1: TEdit
    Left = 184
    Top = 360
    Width = 121
    Height = 21
    TabOrder = 3
    Text = '1'
  end
end
------------------cut----------------

0
 
LVL 9

Expert Comment

by:ITugay
ID: 2704749
Hi dany ,
Not so easy but It work:

SG - your TStringGrid.
2- is column to sort
-------------
procedure TForm1.SpeedButton1Click(Sender: TObject);
var L : TStringList;
    G : TStringGrid;
    I : integer;
begin
   G := TStringGrid.Create(nil);
   G.RowCount:=SG.RowCount;
   G.ColCount:=SG.ColCount;

   L := TStringList.Create;
   L.Sorted:=true;
   for I:=0 to SG.RowCount-1 do
   begin
      L.AddObject(SG.Cells[2,I],pointer(I)); // column to sort is 2
      G.Rows[I].Assign(SG.Rows[I]);
   end;
   for I:=0 to L.Count-1 do
     SG.Rows[I].Assign(G.Rows[integer(L.Objects[I])]);

   L.Free;
   G.Free;
end;
-------------

Cheers,
Igor.
0
 

Author Comment

by:danyb
ID: 2707068
Thanks
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

770 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