Solved

TStringGrid

Posted on 2000-04-10
4
529 Views
Last Modified: 2010-05-18
Need help, how to sort Cols in TStringGrid.
0
Comment
Question by:danyb
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 50 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb submit form as a POST request 4 304
Delphi procedure  - Unsatisfied forward or external declaration. 2 161
Using idhttp to login to instagram 2 89
Delphi 2 59
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

772 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