Dbgrid的多重排序(100分)

  • 主题发起人 主题发起人 yanssunhufeng
  • 开始时间 开始时间
Y

yanssunhufeng

Unregistered / Unconfirmed
GUEST, unregistred user!
dbgrid点列头排序可以实现排序的。但如果实现多重排序?
 
你单列排序是通过sql的order实现吗?
如果是,你多列也可以这样实现吧 ,只是记录下前排已经排序的列名称,顺序加在order by 中排序就是。
 
是的。我试过有些复杂。如果哪位大侠有现成的代码就好了
 
以前的一段代码
procedure SortStringGrid(Grid: TStringGrid; ColID: Integer;
DataType: TGridDataType);
var
TmpOrder: Boolean;
TmpStrList: TStrings;
i: Integer;
{交换行}
procedure MoveStringGridData(Grid: TStringGrid; Sou, Des: Integer);
var
K: Integer;
begin
TmpStrList.Clear;
for K := 0 to Grid.ColCount - 1 do //Grid.FixedCols
TmpStrList.Add(Grid.Cells[K, Sou]);
Grid.Rows[Sou] := Grid.Rows[Des];
for K := 0 to Grid.ColCount - 1 do //Grid.FixedCols
Grid.Cells[K, Des] := TmpStrList.Strings[K];
end;
{快速排序}
procedure QuickSort(Grid: TStringGrid; pLowRow, pHighRow: Integer);
var
LowRow, HighRow: Integer;
MidValue: string;
begin
LowRow := pLowRow;
HighRow := pHighRow;
MidValue := Grid.Cells[ColID, (LowRow + HighRow) div 2];

repeat
case DataType of
gdtString, gdtDateTime:
if TmpOrder then //正序、字符
begin
while Grid.Cells[ColID, LowRow] < MidValue do
Inc(LowRow);
while Grid.Cells[ColID, HighRow] > MidValue do
Dec(HighRow);
end
else
begin //反序、字符
while Grid.Cells[ColID, LowRow] > MidValue do
Inc(LowRow);
while Grid.Cells[ColID, HighRow] < MidValue do
Dec(HighRow);
end;

gdtNumber:
begin
if Grid.Cells[ColID, LowRow] = '' then
Grid.Cells[ColID, LowRow] := '0';
if Grid.Cells[ColID, HighRow] = '' then
Grid.Cells[ColID, HighRow] := '0';
if MidValue = '' then
MidValue := '0';
if TmpOrder then
begin //正序、数字
while StrToFloat(Grid.Cells[ColID, LowRow]) < StrToFloat(MidValue) do
Inc(LowRow);
while StrToFloat(Grid.Cells[ColID, HighRow]) > StrToFloat(MidValue) do
Dec(HighRow);
end
else
begin //反序、数字
while StrToFloat(Grid.Cells[ColID, LowRow]) > StrToFloat(MidValue) do
Inc(LowRow);
while StrToFloat(Grid.Cells[ColID, HighRow]) < StrToFloat(MidValue) do
Dec(HighRow);
end;
end;
end;

if LowRow <= HighRow then
begin
MoveStringGridData(Grid, LowRow, HighRow);
Inc(LowRow);
Dec(HighRow);
end;
until LowRow > HighRow;

if HighRow > pLowRow then
QuickSort(Grid, pLowRow, HighRow);
if LowRow < pHighRow then
QuickSort(Grid, LowRow, pHighRow);
end;

begin
try
TmpStrList := TStringList.Create;
{决定顺序}
case DataType of
gdtString, gdtDateTime:
TmpOrder := Grid.Cells[ColID, Grid.FixedRows] > Grid.Cells[ColID,
Grid.FixedRows + 1];
gdtNumber:
for i := 0 to 254 do
begin
if StrToFloat(Grid.Cells[ColID, Grid.FixedRows + i]) =
StrToFloat(Grid.Cells[ColID, Grid.FixedRows + i + 1]) then
Continue
else
begin
TmpOrder := StrToFloat(Grid.Cells[ColID, Grid.FixedRows + i]) >
StrToFloat(Grid.Cells[ColID, Grid.FixedRows + i + 1]);
break;
end;
end;
end;

try
QuickSort(Grid, Grid.FixedRows, Grid.RowCount - 1);
except
on E: Exception do
raise;
end;
finally
TmpStrList.Free;
end;
end;
 
你参考吧,刚写的。
var sel_no:integer; //当前选择的列数目
sel_col_name:array[1..10] of string; //排序列名称

procedure TForm1.DBGrid1TitleClick(Column:TColumn);
var i:integer;
order_str:WideString;
begin
for i:=1 to sel_no do
if Column.Title.Caption=sel_col_name then exit; //该列已经存在
Inc(sel_no);
sel_col_name[sel_no]:=Column.Title.Caption;
for i:=1 to sel_no do //生成order ...
if i=1 then
order_str:=' order by '+sel_col_name
else
order_str:=order_str+','+sel_col_name;
query1.close;
query1.sql.text:=tsql+ order_str; //tsql是select * from tablename where ... 除排序之外的内容
query1.open;
end;
 
我这里的估计可以帮到你
blog.csdn.net/stephenewong
 
后退
顶部