转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
StringGrid使用全书( 一)         ★★★★

StringGrid使用全书( 一)

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1725 更新时间:2009/4/23 18:30:18
排序procedure GridQuickSort(Grid: TStringGrid; ACol: Integer; Order: Boolean ; NumOrStr: Boolean);
(******************************************************************************)
(*  函数名称:GridQuickSort                                                   *)
(*  函数功能:给 StringGrid 的 ACol 列快速法排序    _/_/     _/_/  _/_/_/_/_/ *)
(*  参数说明:                                          _/   _/        _/      *)
(*            Order: True 从小到大                       _/          _/       *)
(*                 : False 从大到小                     _/          _/        *)
(*        NumOrStr : true 值的类型是Integer          _/_/        _/_/         *)
(*                 : False 值的类型是String                                   *)
(*  函数说明:对于日期,时间等类型数据均可按字符方式排序,                    *)
(*                                                                            *)
(*                                                                            *)(******************************************************************************)
procedure MoveStringGridData(Grid: TStringGrid; Sou,Des :Integer );
var
  TmpStrList: TStringList ;
  K : Integer ;
begin
  try
    TmpStrList :=TStringList.Create() ;
    TmpStrList.Clear ;
    for K := Grid.FixedCols to Grid.ColCount -1 do
      TmpStrList.Add(Grid.Cells[K,Sou]) ;
    Grid.Rows [Sou] := Grid.Rows [Des] ;
    for K := Grid.FixedCols to Grid.ColCount -1 do
      Grid.Cells [K,Des]:= TmpStrList.Strings[K] ;
  finally
    TmpStrList.Free ;
  end;
end;

procedure QuickSort(Grid: TStringGrid; iLo, iHi: Integer);
var
  Lo, Hi : Integer;
  Mid: String ;
begin
  Lo := iLo ;
  Hi := iHi ;
  Mid := Grid.Cells[ACol,(Lo + Hi) div 2];
  repeat
    if Order and not NumOrStr then //按正序、字符排
    begin
      while Grid.Cells[ACol,Lo] < Mid do Inc(Lo);
      while Grid.Cells[ACol,Hi] > Mid do Dec(Hi);
    end ;
    if not Order and not NumOrStr then //按反序、字符排
    begin
      while Grid.Cells[ACol,Lo] > Mid do Inc(Lo);
      while Grid.Cells[ACol,Hi] < Mid do Dec(Hi);
    end;

    if NumOrStr then
    begin
      if Grid.Cells[ACol,Lo] = '''''''' then Grid.Cells[ACol,Lo] := ''''0'''' ;
      if Grid.Cells[ACol,Hi] = '''''''' then Grid.Cells[ACol,Hi] := ''''0'''' ;
      if Mid = '''''''' then Mid := ''''0'''' ;
      if Order then
      begin //按正序、数字排
        while StrToFloat(Grid.Cells[ACol,Lo]) < StrToFloat(Mid) do Inc(Lo);
        while StrToFloat(Grid.Cells[ACol,Hi]) > StrToFloat(Mid) do Dec(Hi);
      end else
      begin //按反序、数字排
        while StrToFloat(Grid.Cells[ACol,Lo]) > StrToFloat(Mid) do Inc(Lo);
        while StrToFloat(Grid.Cells[ACol,Hi]) < StrToFloat(Mid) do Dec(Hi);
      end;
    end ;
    if Lo <= Hi then
    begin
      MoveStringGridData(Grid, Lo, Hi) ;
      Inc(Lo);
      Dec(Hi);
    end;
  until Lo > Hi;
  if Hi > iLo then QuickSort(Grid, iLo, Hi);
  if Lo < iHi then QuickSort(Grid, Lo, iHi);
end;

begin
try
  QuickSort(Grid, Grid.FixedRows, Grid.RowCount - 1 ) ;
except
on E: Exception do
  Application.MessageBox(Pchar(''''系统在排序数据的时候遇到异常:''''#13+E.message+#13''''请重试,如果该问题依然存在请与程序供应商联系!''''),''''系统错误'''',MB_OK+MB_ICONERROR) ;
end;
end;

procedure StringGridTitleDown(Sender: TObject;
Button: TMouseButton;  X, Y: Integer);
(******************************************************************************)
(*  函数名称:StringGridTitleDown                                             *)
(*  函数功能:取鼠标点StringGrid 的列                _/_/     _/_/  _/_/_/_/_/ *)
(*  参数说明:                                          _/   _/        _/      *)
(*            Sender                                     _/          _/       *)
(*                                                (*                                                                            
  (******************************************************************************)
var
I: Integer ;
begin
if (Y > 0 ) and (y < TStringGrid(Sender).DefaultRowHeight * TStringGrid(Sender).FixedRows ) then
begin
  if  Button = mbLeft then
  begin
    I := X div  TStringGrid(Sender).DefaultColWidth ;
    //这个i 就是要排序得行了
    // 下面调用上面的排序函数就可以了,
    GridQuickSort(TStringGrid(Sender), I, False, True) ;
  end;
end;
end;

   用上面的两个函数就能解决你的问题了。在TStringGrid 的MouseDown事件中调用StringGridTitleDown 函数就可以。你可能要修改一下StringGridTitleDown函数来修改排序得方式及其字符类型。
   提醒你一下对于日期、时间、布尔等类型数据均可按字符方式排序。
例如:

procedure TForm_Main.StringGrid1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
StringGridTitleDown(Sender,Button,X,Y);
end;

上一页  [1] [2] 


[常用软件]评测:Gmail Beta(一)  [VB.NET程序]把握VB.NET中的流(Stream) (一)
[VB.NET程序]从Csharp走到VB.Net(一):MyClass保留字  [Delphi程序]一 个 实 用 的Delphi 屏 幕 拷 贝 程 序 的 设
[Delphi程序]Multi-Tier结构程序开发基础教程 (一)  [Delphi程序]游戏脚本管理 (一)
[Delphi程序]Integer GUID和Comb做主键的效率测试(Delphi+acce…  [网页制作]DreamwaverMX与ASP.NET(一)
[Web开发]XPDL学习与分享 一  [Web开发]ADO.net学习记录 (一)
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台