Solved

TStringGrid

Posted on 2000-04-10
4
534 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 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

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!

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

740 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