stringgrid怎样才能列排序(点一下列名,按值的大小重排)?

antimicro 2002-07-16 11:29:14
我现在怎么根本点不了列?
...全文
118 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
王集鹄 2002-07-16
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Menus, ComCtrls, Mask, Grids;

type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

(*//
标题:字符网格排序
说明:升序、降序;示例点击标题排序
设计:Zswang
日期:2002-04-27
支持:wjhu111@21cn.com
//*)

///////Begin Source
function StringGridRowSwap(mStringGrid: TStringGrid;
mFromRow, mToRow: Integer): Boolean;
var
S: string;
begin
Result := False;
if (mToRow = mFromRow) then Exit;
if not Assigned(mStringGrid) then Exit;
if (mFromRow < 0) or (mFromRow >= mStringGrid.RowCount) then Exit;
if (mToRow < 0) or (mToRow >= mStringGrid.RowCount) then Exit;
try
S := mStringGrid.Rows[mFromRow].Text;
mStringGrid.Rows[mFromRow].Text := mStringGrid.Rows[mToRow].Text;
mStringGrid.Rows[mToRow].Text := S;
except
Exit;
end;
Result := True;
end; { StringGridRowSwap }

function StringGridRowSort(mStringGrid: TStringGrid;
mColIndex: Integer; mDesc: Boolean = False): Boolean;
var
I, J: Integer;
begin
Result := False;
if not Assigned(mStringGrid) then Exit;
if (mColIndex < 0) or (mColIndex >= mStringGrid.ColCount) then Exit;
for I := mStringGrid.FixedRows to mStringGrid.RowCount - 2 do
for J := I + 1 to mStringGrid.RowCount - 1 do
if mDesc then
if mStringGrid.Cells[mColIndex, I] < mStringGrid.Cells[mColIndex, J] then
StringGridRowSwap(mStringGrid, I, J)
else
else if mStringGrid.Cells[mColIndex, I] > mStringGrid.Cells[mColIndex, J] then
StringGridRowSwap(mStringGrid, I, J);
Result := True;
end; { StringGridRowSort }
///////End Source

///////Begin Demo
procedure TForm1.StringGrid1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
{$J+}
const
vOldCol: Integer = -1;
{$J-}
var
vCol, vRow: Integer;
begin
if Button = mbRight then Exit;
TStringGrid(Sender).MouseToCell(X, Y, vCol, vRow);
if (vRow < 0) or (vRow >= TStringGrid(Sender).FixedRows) then Exit;
StringGridRowSort(TStringGrid(Sender), vCol, vOldCol = vCol);
if vOldCol = vCol then
vOldCol := - vOldCol
else vOldCol := vCol;
end;
///////End Demo

end.
antimicro 2002-07-16
  • 打赏
  • 举报
回复
alec_ma(乐无边):
tms是什么啊?在哪里?
alec_ma 2002-07-16
  • 打赏
  • 举报
回复
tms
antimicro 2002-07-16
  • 打赏
  • 举报
回复
有没有自动排序的类似grid的控件啊?

5,939

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧