最近在做stringgrid的项目, 下面delphi7 正常使用,均摘抄网路,但做过细微调整才能正常使用

首先排序的过程

procedure Quicksort(Grid: TStringGrid; var List: array of integer; min, max, sortcol, datatype: Integer);
{List is a list of rownumbers in the grid being sorted}
var
  med_value: integer;
  hi, lo, i: Integer;

function compare(val1, val2: string): integer;
  var
    int1, int2: integer;
    float1, float2: extended;
    errcode: integer;
  begin
    case datatype of
      0:
        result := ANSIComparetext(val1, val2);
      1:
        begin
          int1 := strtointdef(val1, 0);
          int2 := strtointdef(val2, 0);
          if int1 > int2 then
            result := 1
          else if int1 < int2 then
            result := -1
          else
            result := 0;
        end;
      2:
        begin
          val(val1, float1, errcode);
          if errcode <> 0 then
            float1 := 0;
          val(val2, float2, errcode);
          if errcode <> 0 then
            float2 := 0;
          if float1 > float2 then
            result := 1
          else if float1 < float2 then
            result := -1
          else
            result := 0;
        end;
    else
      result := 0;
    end;
  end;

begin
{If the list has <= 1 element, it's sorted}
  if (min >= max) then
    Exit;
{Pick a dividing item randomly}
  i := min + Trunc(Random(max - min + 1));
  med_value := List[i];
  List[i] := List[min]; { Swap it to the front so we can find it easily}
{Move the items smaller than this into the left
half of the list. Move the others into the right}
  lo := min;
  hi := max;
  while (True) do
  begin
// Look down from hi for a value < med_value.
    while compare(Grid.cells[sortcol, List[hi]], grid.cells[sortcol, med_value]) >= 0 do
(*ANSIComparetext(Grid.cells[sortcol,List[hi]]
,grid.cells[sortcol,med_value])>=0 do*)
    begin
      hi := hi - 1;
      if (hi <= lo) then
        Break;
    end;
    if (hi <= lo) then
    begin {We're done separating the items}
      List[lo] := med_value;
      Break;
    end;
// Swap the lo and hi values.
    List[lo] := List[hi];
    inc(lo); {Look up from lo for a value >= med_value}
    while Compare(grid.cells[sortcol, List[lo]], grid.cells[sortcol, med_value]) < 0 do
    begin
      inc(lo);
      if (lo >= hi) then
        break;
    end;
    if (lo >= hi) then
    begin {We're done separating the items}
      lo := hi;
      List[hi] := med_value;
      break;
    end;
    List[hi] := List[lo];
  end;
{Sort the two sublists}
  Quicksort(Grid, List, min, lo - 1, sortcol, datatype);
  Quicksort(Grid, List, lo + 1, max, sortcol, datatype);
end;
//datatype 0:按字符排序 1:按整型排序 2:按浮点型排序

procedure Tfrmbase.Sortgrid(var Grid : TStringGrid; sortcol,datatype:integer);
var
   i : integer;
   tempgrid:tstringGrid;
   list:array of integer;
begin
  screen.cursor:=crhourglass;
  tempgrid:=TStringgrid.create(nil);
  with tempgrid do
  begin
    rowcount:=grid.rowcount;
    colcount:=grid.colcount;
    fixedrows:=grid.fixedrows;
  end;

setlength(list,Grid.rowcount-Grid.fixedrows);
    for i:= Grid.fixedrows to Grid.rowcount-1 do
    begin
      list[i-Grid.fixedrows]:=i;
      tempgrid.rows[i].AddStrings(grid.rows[i]);
    end;
    quicksort(Grid, list,0,Grid.rowcount-Grid.fixedrows-1,sortcol,datatype);
    for i:=0 to Grid.rowcount-Grid.fixedrows-1 do
    begin
      Grid.rows[i+Grid.fixedrows].Text := '';
      Grid.rows[i+Grid.fixedrows].AddStrings(tempgrid.rows[list[i]]);
    end;
    Grid.row:=Grid.fixedrows;
  tempgrid.free;
  setlength(list,0);
  screen.cursor:=crdefault;
end;

列头排序

procedure Tfrmtranstat.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  c: integer;
  w: integer;
  Grid: TStringGrid;
begin                  //单击排序
  Grid := Sender as TStringGrid;
  with Grid do
    if y <= rowheights[0] then
    begin
      c := 0;
      w := colwidths[0];
      while (c < colcount) and (w <= x) do
      begin
        inc(c);
        w := w + colwidths[c] + gridlinewidth;
      end;
      sortgrid(Grid, c, 0);
    end;
end;

http://blog.csdn.net/y281252548/article/details/52527807

delphi7 stringgrid 点列头排序的更多相关文章

  1. easyui datagrid 点击列表头排序出现错乱的原因

    之前我的导师,也就是带我的同事,使用datagrid,发现点击列表头排序出现乱序,按理说只有顺序和逆序两种排序结果.因为他比较忙,当时没解决,把排序禁掉了,后来又要求一定要排序,所以他交给我. 一开始 ...

  2. [WPF]ListView点击列头排序功能实现

    [转]   [WPF]ListView点击列头排序功能实现 这是一个非常常见的功能,要求也很简单,在Column Header上显示一个小三角表示表示现在是在哪个Header上的正序还是倒序就可以了. ...

  3. Delphi7中ClientDataSet的排序

    http://eteda.iteye.com/blog/1141312 Delphi7中ClientDataSet的排序 博客分类: Delphi   1.控件ClientDataSet的属性Inde ...

  4. MFC listcontrol 分列 添加行数据 点击列头排序

    适用于 对话框程序 1.在工具箱中拖出 ListControl,然后右键-属性,view-Report 让你的ListControl变成这幅模样! 2.添加ListControl控件的control类 ...

  5. 交叉报表列头排序时遇到的oracle问题—oracle ORA-12704:字符集不匹配、varchar2转化为nvarchar2字符缺失、case when else后的字符类型要一致

    在做交叉报表列头的排序时,遇到这三个问题,下面具体来说一下. 设计的数据库的表结构如图1所示: 图1 要处出来student_name_,s.grade_,s.subject_name_,这三个属性, ...

  6. C++ 简单实现MFC ListControl 点击列头排序

    说明: SetItemData可以为每一行绑定一个DWORD类型的变量.用GetItemData可以获得这个变量.举个例子,假设CListCtrl中你需要显示某个数据表中的记录,该表有个流水号主键ID ...

  7. MFC CListControl 点击列头排序的实现

    SetItemData可以为每一行绑定一个DWORD类型的变量.用GetItemData可以获得这个变量.举个例子,假设CListCtrl中你需要显示某个数据表中的记录,该表有个流水号主键ID,一般这 ...

  8. DataWindow.NET 控件 实现点击列头排序

    1.定义字段                         Boolean ib_SetSort = true;                string is_SortType = " ...

  9. 点击Listview列头排序

    Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader) ListView1.Sorted = ...

随机推荐

  1. 循环不变式(loop invariant)

    循环不变式是一种条件式(必须满足的条件,对循环而言是保持不变的,无论循环执行了多少次),循环语句没执行一次,就要求中间的结果必须符合不变式的要求. (1)进入循环语句时,不变式必须成立: (2)循环语 ...

  2. Erlang epmd的角色以及使用

    本文链接地址: Erlang epmd的角色以及使用 很多同学误会了epmd的作用,认为epmd就是erlang集群的协议,我来澄清下: Epmd是Erlang Port Mapper Daemon的 ...

  3. PostThreadMessage发送进程间消息(对话框向控制台发消息,控制台也可有消息循环)

    函数原型 BOOL PostThreadMessage( DWORD idThread, UINT Msg, WPARAM wParam, LPARAM lParam ); 1 2 3 4 5 6 T ...

  4. CSRF的攻击与防御

    CSRF(Cross-site request forgery)跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XSRF,是一种对网站 ...

  5. C# Span 入门

    原文:C# Span 入门 版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问.如果当前博客图片看不到,请到 http://lindexi.gitee.io 访问博客.本 ...

  6. C#--动态操作DataTable

    C#动态操作DataTable(新增行.列.查询行.列等) 方法一:动态创建一个DataTable ,并为其添加数据 public void CreateTable()        {        ...

  7. 解决:insert Vodafone sim card,open the mms read report,when receive the read report,cann&#39;t download..

    insert Vodafone sim card,open the mms read report,when receive the read report,cann't download the m ...

  8. tinkerpop(1) 地图数据库console科研

    原创文章连接: http://blog.csdn.net/freewebsys/article/details/46348975 转载请注明出处. 1,关于图数据库 tinkerpop是apache孵 ...

  9. springboot 配置多数据源 good

    1.首先在创建应用对象时引入autoConfig package com; import org.springframework.boot.SpringApplication; import org. ...

  10. WPF图形/文字特别效果之一:交叉效果探讨

    原文:WPF图形/文字特别效果之一:交叉效果探讨 为了说明问题,先看下图:图1  完全重叠的单一颜色文字它是2008几个字的叠加,并且颜色为单一的红色.如果不仔细分辨,你或许无法一下子看出是2008. ...