delphi编程, 如何实现赋值StringGrid表格内的数字按顺序排列

要达到这种效果是我弄错图片了
2025-03-01 22:17:56
推荐回答(3个)
回答1:

从小到大排列,用个简单的冒泡排序法就好了。
procedure TForm1.Button1Click(Sender: TObject);
var
i,j:Integer;
tmp:Double;
begin
for i:=0 to StringGrid1.RowCount-2 do
begin
if StringGrid1.Cells[0,i]='' then continue;
for j:=i to StringGrid1.RowCount -2 do
begin
if StrToFloat(StringGrid1.Cells[0,j]) > StrToFloat(StringGrid1.Cells[0,j+1]) then
begin
tmp:=StrToFloat(StringGrid1.Cells[0,j+1]);
StringGrid1.Cells[0,j+1] := StringGrid1.Cells[0,j];
StringGrid1.Cells[0,j]:=FloatToStr(tmp);
end;
end;
end;
end;

回答2:

弄一数据库,搞一个表,字段为数字类型,界面用一个DBGRID,ADO数据连接组件中,指明排序方式就行了。冒泡排序,还要重写StringGrid挺麻烦的。

回答3:

你两个图没区别啊