delphi StringGrid单击表头进行高速排序

unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DB, ADODB, DBGrids, ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    ADOConnection1: TADOConnection;
    ADOTable1: TADOTable;
    Panel1: TPanel;
    DataSource1: TDataSource;
    Button1: TButton;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure FormCreate(Sender: TObject);
    procedure StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

//排序函数//
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 Sortgrid(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;
  with Grid do
  begin
    setlength(list,rowcount-fixedrows);
    for i:= fixedrows to rowcount-1 do
    begin
      list[i-fixedrows]:=i;
      tempgrid.rows[i].assign(grid.rows[i]);
    end;
    quicksort(Grid, list,0,rowcount-fixedrows-1,sortcol,datatype);
    for i:=0 to rowcount-fixedrows-1 do
    begin
      rows[i+fixedrows].assign(tempgrid.rows[list[i]])
    end;
    row:=fixedrows;
  end;
  tempgrid.free;
  setlength(list,0);
  screen.cursor:=crdefault;
end;


{$R *.dfm}

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
    NewColor:TColor;
begin
{    Randomize();
    NewColor:=RGB(Random(255),Random(255),Random(255));
    self.StringGrid1.Canvas.Brush.Color:=NewColor;
    self.StringGrid1.Canvas.FillRect(Rect);
    self.StringGrid1.Canvas.TextOut(Rect.Left+2,Rect.Top+2,self.StringGrid1.Cells[ACol,ARow]);
     }
     with StringGrid1.Canvas do   //隔行着色
 begin

   if (ARow mod 2=0) and (ARow>1) and(ACol>0) then
    begin
     Brush.Color:=clInfoBk;
   end
   else
   begin
     if ACol=0 then    Brush.Color:=cl3DLight else
      Brush.Color:=clWhite;
   end;
   if ARow=0 then  Brush.Color:=cl3DLight;

   font.color:=clBlack;
   FillRect(Rect);
   textout(Rect.left,Rect.top,StringGrid1.Cells[ACol,ARow]);

    end;
 end;

procedure TForm1.FormCreate(Sender: TObject);
var
    i,j:Integer;
begin            //初始货
    adotable1.First;
    StringGrid1.RowCount:=adotable1.RecordCount+1;
    StringGrid1.ColCount:=adotable1.FieldCount+1;
    for i:=0 to adotable1.RecordCount-1 do
    begin
        for j:=0 to adotable1.FieldCount-1 do
        begin
           StringGrid1.Cells[j+1,0]:=adotable1.Fields.Fields[j].FieldName;
           StringGrid1.Cells[0,i+1]:=IntToStr(i+1);
           if adotable1.Fields.Fields[j].Value=null
              then StringGrid1.Cells[j+1,i+1]:=''
              else StringGrid1.Cells[j+1,i+1]:=adotable1.Fields.Fields[j].Value;
        end;
        adotable1.Next;
    end;
end;

procedure TForm1.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;

end.


版权声明:本文为appleric原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。