sorting a stringgrid

I would like to sort my stringgrid into the correct alphabetical order of the items in the second column. So far i have the following code which sorts the items in the second column, but i do not know how to make it sort the whole row as opposed to only the items in the 2nd column. As a result, the infomation in my stringgrid becomes jumbled up. Could anybody help me in this?

[code]
procedure TForm1.BtnSortClick(Sender: TObject);
begin
for i:=1 to 8 do
for j:=0 to 7 do
if (form1.stringgrid1.cells[2,j]) > (form1.stringgrid1.cells[2,j+1])then
begin
tempstorage:=(form1.stringgrid1.cells[2,j+1]);
form1.stringgrid1.cells[2,j+1]:=(form1.stringgrid1.cells[2,j]);
form1.stringgrid1.cells[2,j]:=tempstorage;
end;
end;
[/code]

Comments

  • : I would like to sort my stringgrid into the correct alphabetical order of the items in the second column. So far i have the following code which sorts the items in the second column, but i do not know how to make it sort the whole row as opposed to only the items in the 2nd column. As a result, the infomation in my stringgrid becomes jumbled up. Could anybody help me in this?
    :
    : [code]
    : procedure TForm1.BtnSortClick(Sender: TObject);
    : begin
    : for i:=1 to 8 do
    : for j:=0 to 7 do
    : if (form1.stringgrid1.cells[2,j]) > (form1.stringgrid1.cells[2,j+1])then
    : begin
    : tempstorage:=(form1.stringgrid1.cells[2,j+1]);
    : form1.stringgrid1.cells[2,j+1]:=(form1.stringgrid1.cells[2,j]);
    : form1.stringgrid1.cells[2,j]:=tempstorage;
    : end;
    : end;
    : [/code]
    :
    Here is an untested code based on your code. The bold parts are add to your code:
    : [code]
    : procedure TForm1.BtnSortClick(Sender: TObject);
    : begin
    : for i:=1 to 8 do
    : for j:=0 to 7 do
    : if (form1.stringgrid1.cells[2,j]) > (form1.stringgrid1.cells[2,j+1])then
    : begin
    [b] for col := 0 to stringgrid1.ColCount-1 do
    begin // switch the entire row 1 string at a time[/b]
    tempstorage:=(form1.stringgrid1.cells[[b]col[/b],j+1]);
    : form1.stringgrid1.cells[[b]col[/b],j+1]:=(form1.stringgrid1.cells[[b]col[/b],j]);
    : form1.stringgrid1.cells[[b]col[/b],j]:=tempstorage;
    [b] end[/b]
    : end;
    : end;
    : [/code]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion