Solved

TStringGrid

Posted on 2000-04-10
4
523 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
Comment Utility
Try TAdvStringGrid from

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

Accepted Solution

by:
DValery earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

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…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

13 Experts available now in Live!

Get 1:1 Help Now