Beranda > Delphi > Sort a StringGrid

Sort a StringGrid

procedure SortStringGrid(var GenStrGrid: TStringGrid; ThatCol: Integer);
const
TheSeparator = ‘@’;
var
CountItem, I, J, K, ThePosition: integer;
MyList: TStringList;
MyString, TempString: string;
begin
CountItem := GenStrGrid.RowCount;
MyList        := TStringList.Create;
MyList.Sorted := False;
try
begin
for I := 1 to (CountItem – 1) do
MyList.Add(GenStrGrid.Rows[I].Strings[ThatCol] + TheSeparator +
GenStrGrid.Rows[I].Text);
Mylist.Sort;
for K := 1 to Mylist.Count do
begin
MyString := MyList.Strings[(K – 1)];
ThePosition := Pos(TheSeparator, MyString);
TempString  := ”;
{Eliminate the Text of the column on which we have sorted the StringGrid}
TempString := Copy(MyString, (ThePosition + 1), Length(MyString));
MyList.Strings[(K – 1)] := ”;
MyList.Strings[(K – 1)] := TempString;
end;
for J := 1 to (CountItem – 1) do
GenStrGrid.Rows[J].Text := MyList.Strings[(J – 1)];
end;
finally
MyList.Free;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
SortStringGrid(StringGrid1, 1);
end;

Iklan
Kategori:Delphi
  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: