抢分啦!:网上的那位朋友有<=能够排序的DBGRID控件=>,分不够可以再加!谢谢各位了!(50分)

  • 主题发起人 主题发起人 fdwangchao
  • 开始时间 开始时间
F

fdwangchao

Unregistered / Unconfirmed
GUEST, unregistred user!
各位:
因小弟最近在做一些东东,需要用到能够排序的DBGRID控件,如果你们有这种控件的话,
希望朋友们能发一个给我,或提供相关的下载网站,在这里谢谢大家啦!
E-Mail:fdwangchao@sina.com或fdwangchao@163.com
 
DBGRID自己就可以
procedure TForm4.DBGrid4TitleClick(Column: TColumn);
begin
with ryADOTable1 do
begin
if DBGrid1Boolean then
TADOTable(ryADOTable1).Sort := Column.FieldName + ' DESC'
else
TADOTable(ryADOTable1).Sort := Column.FieldName;
DBGrid1Boolean := not(DBGrid1Boolean);
end;
end;
 
请参阅:
C:/Program Files/Borland/Delphi6/Demos/Ado/AdoTest/AdoMain.pas
中的procedure TADODBTest.GridTitleClick(Column: TColumn);
 
同意xj_liyq
 
dev控建可以。但太大了。
 
没有任何DBGrid可以永久性的解决排序问题,因为你不知道
DBGird.DataSourec.DataSet是什么,TTable,TQuery,TClientDataSet,或者是
其它的第三方控件,所以你需要解决的不是DBGird的排序,而是DBGird.DataSourec.DataSet
的排序。这里有一段代码你可以参考:

//==============================================================================
//点击DBGrid标题栏对查询结果排序************************************************
//==============================================================================
function DBGridTitleOrder(Column: TColumn): integer;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//DBGrid.DataSource.DataSet is TTable*****************************************
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TableOrder;
begin

end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//DBGrid.DataSource.DataSet is TQuery*****************************************
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure QueryOrder;
var
SQLStr, OrderFieldName, TempStr: string;
i, OrderPos: integer;
SavedParams: TParams;
begin
if not (TQuery(TDBGrid(Column.Grid).DataSource.DataSet).State in [dsBrowse]) then
Exit;
SavedParams := nil;
for i:=0 to Column.Grid.FieldCount-1 do
begin
{TDBGrid(Column.Grid).Columns.Font.Color := clBlack;}
TDBGrid(Column.Grid).Columns.Title.Caption := TDBGrid(Column.Grid).Columns.Field.DisplayName;
end;
if not (Column.Field.FieldKind in [fkData,fkLookup]) then
Exit;
if Column.Field.FieldKind=fkData
then
OrderFieldName := LowerCase(Column.Field.FieldName)
else
OrderFieldName := LowerCase(Column.Field.KeyFields);
while Pos(OrderFieldName,';')<>0 do
OrderFieldName := copy(OrderFieldName,1,Pos(OrderFieldName,';')-1)+','+copy(OrderFieldName,Pos(OrderFieldName,';')+1,100);
with TQuery(TDBGrid(Column.Grid).DataSource.DataSet) do
begin
SQLStr := LowerCase(SQL.Text);
if ParamCount>0 then
begin
SavedParams := TParams.Create;
SavedParams.Assign(Params);
end;
OrderPos := pos('order',SQLStr);
if (OrderPos=0) or (pos(OrderFieldName,copy(SQLStr,OrderPos,100))=0) then
begin
Result := 1;
TempStr := ' order by ' + OrderFieldName + ' asc';
{Column.Title.Caption := Column.Title.Caption + '(▲)';}
{Column.font.Color := clRed;}
end else
if pos('asc',SQLStr)=0 then
begin
Result := 1;
TempStr := ' order by ' + OrderFieldName + ' asc';
{Column.Title.Caption := Column.Title.Caption + '(▲)';}
{Column.font.Color := clRed;}
end else
begin
Result := 2;
TempStr := ' order by ' + OrderFieldName + ' desc';
{Column.Title.Caption := Column.Title.Caption + '(▼)';}
{Column.font.Color := clGreen;}
end;
if OrderPos<>0 then
SQLStr := Copy(SQLStr,1,OrderPos-1);
SQLStr := SQLStr + TempStr;
Active := False;
SQL.Clear;
SQL.Text := SQLStr;
if ParamCount>0 then
begin
Params.AssignValues(SavedParams);
SavedParams.Free;
end;
Open;
end;
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//DBGrid.DataSource.DataSet is TADODataSet************************************
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure ADODataSetOrder;
begin
with (TDBGrid(Column.Grid).DataSource.DataSet as TCustomADODataSet) do
begin
if (Pos(Column.Field.FieldName, Sort)=1)
and (Pos(' DESC', Sort)=0) then
begin
Result := 2;
Sort := Column.Field.FieldName + ' DESC'
end else
begin
Result := 1;
Sort := Column.Field.FieldName + ' ASC';
end;
end;
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//DBGrid.DataSource.DataSet is TClientDataSet*********************************
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure ClientDataSetOrder;
begin
TDBGrid(Column.Grid).Tag := TDBGrid(Column.Grid).Tag xor Round(Exp(Column.Index * Ln(2)));
with (TDBGrid(Column.Grid).DataSource.DataSet as TClientDataSet) do
begin
if Column.Field.DataType<ftAutoInc then
begin
if (TDBGrid(Column.Grid).Tag and Round(Exp(Ln(2) * Column.Index)))=0 then
begin
AddIndex(Column.FieldName + 'InxDES', Column.FieldName, [ixDescending]);
IndexName := Column.FieldName + 'InxDES';
Result := 2;
end else
begin
AddIndex(Column.FieldName + 'InxASC', Column.FieldName, []);
IndexName := Column.FieldName + 'InxASC';
Result := 1;
end;
end;
end;
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
begin
if (TDBGrid(Column.Grid).DataSource.DataSet is TTable) then
TableOrder
else
if (TDBGrid(Column.Grid).DataSource.DataSet is TQuery) then
QueryOrder
else
if (TDBGrid(Column.Grid).DataSource.DataSet is TADODataSet) then
ADODataSetOrder
else
if (TDBGrid(Column.Grid).DataSource.DataSet is TClientDataSet) then
ClientDataSetOrder;
end;
 
用DevExpress_ QuantumGrid 这个可是最好用的,我在用紧,唔使写代码.
在51delphi中有
 
在ontitleclick里,加入 Table2.IndexFieldNames:= Column.FieldName;
 
多人接受答案了。
 
后退
顶部