怎样实现点击dbgrid列数据按升降排序(不使用dataset的排序功能)(30分)

  • 主题发起人 主题发起人 jeffsheng
  • 开始时间 开始时间
J

jeffsheng

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样实现点击dbgrid列数据按升降排序(不使用dataset的排序功能)
 
觉的这个问题没有意义。
 
给你一段基于C/S三层结构中的这段代码:

通用过程


//=====================================================================
procedure TfmAgentManager.GridTitleSort(Column: TColumn);
var s,cFieldName:string;
i:integer;
DataSet:TDataSet;
procedure setTitle;
var ii:integer;
cStr:string;
c:TColumn;
begin
for ii:=0 to TDBGrid(Column.Grid).Columns.Count-1 do
begin
c:=TDBGrid(Column.Grid).Columns[ii];
cStr:=c.Title.Caption;
if (pos('▲',cStr)=1) or (pos('▼',cStr)=1) then
begin
Delete(cStr,1,2);
c.Title.Caption:=cStr;
end;
end;

end;
begin
setTitle;
DataSet:=Column.Grid.DataSource.DataSet;
if Column.Field.FieldKind=fkLookup then
cFieldName:=Column.Field.KeyFields
else if Column.Field.FieldKind=fkCalculated then
cFieldName:=Column.Field.KeyFields
else
cFieldName:=Column.FieldName;
//=================================AdoDataSet=====================

if DataSet is TCustomADODataSet then begin
s:=TCustomADODataSet(DataSet).Sort;
if s='' then begin
s:=cFieldName;
Column.Title.Caption:='▲'+Column.Title.Caption;
end
else begin
if Pos(cFieldName,s)<>0 then begin
i:=Pos('DESC',s);
if i<=0 then begin
s:=s+' DESC';
Column.Title.Caption:='▼'+Column.Title.Caption;
end
else begin
Column.Title.Caption:='▲'+Column.Title.Caption;
Delete(s,i,4);
end;
end
else begin
s:=cFieldName;
Column.Title.Caption:='▲'+Column.Title.Caption;
end;
end;
TCustomADODataSet(DataSet).Sort:=s;
end
//============================Clientdataset==========================
else if DataSet is TClientDataSet then begin
if TClientDataSet(DataSet).indexfieldnames<>'' then
begin
i:=TClientDataSet(DataSet).IndexDefs.IndexOf('i'+Column.FieldName);
if i=-1 then
begin
with TClientDataSet(DataSet).IndexDefs.AddIndexDef do
begin
Name:='i'+Column.FieldName;
Fields:=Column.FieldName;
DescFields:=Column.FieldName;
end;
end;
TClientDataSet(DataSet).IndexFieldNames:='';
TClientDataSet(DataSet).IndexName:='i'+Column.FieldName;
Column.Title.Caption:='▼'+Column.Title.Caption;
end
else
begin
TClientDataSet(DataSet).IndexName:='';
TClientDataSet(DataSet).IndexFieldNames:=column.fieldname;
Column.Title.Caption:='▲'+Column.Title.Caption;
end;
end;
end;
//======================================================================


procedure TfmAgentManager.DBGrid1TitleClick(Column: TColumn);
begin
GridTitleSort(column);
end;
// 调用-----------------------------------------------------------------------
 
如果对这段代码有什么问题 请post出来
这是我的一个程序里使用的
能实现你说的功能
 
OK,我先试试!谢谢
 
写成控件嘛,应该没难度的。
 
xyue:
我進行了以下修改:
1。当字段类型为ftMemo ,ftGraphic等时会出错,当然这些类型的字段一般不会放在Grid中,
但是还是判断一下为妙:)
2。因为我用的是InfoPower,而InfoPower的wwDBGrid的onTitleButtonClick事件和普通的
DBGrid的OnTitleClick事件并不相同,所以我加以修改,添加了一个For InforPower
的wwGridTitleSort过程。
两段都贴在下面,兄弟们提意见。
另:感谢xyue兄,这段代码我马上用到了!!Thanks very much!!

procedure GridTitleSort(Column: TColumn);
Type
TFieldTypeSet = set of TFieldType;
var
s, cFieldName: string;
i: integer;
DataSet: TDataSet;
GridFieldTypeSet: TFieldTypeSet;
procedure SetTitle;
var
ii: integer;
cStr: string;
c: TColumn;
begin
for ii := 0 to TDBGrid(Column.Grid).Columns.Count - 1 do
begin
c := TDBGrid(Column.Grid).Columns[ii];
cStr := c.Title.Caption;
if (pos('¡¶', cStr) = 1) or (pos('¡¿', cStr) = 1) then
begin
Delete(cStr, 1, 2);
c.Title.Caption := cStr;
end;
end;
end;
begin
SetTitle;
DataSet := Column.Grid.DataSource.DataSet;

GridFieldTypeSet := [ftString, ftSmallint, ftInteger, ftWord,
ftBoolean, ftFloat, ftCurrency, ftBCD, ftDate,
ftTime, ftDateTime, ftBytes, ftVarBytes,
ftTypedBinary, ftFixedChar, ftWideString,
ftLargeint, ftVariant];
if not (Column.Field.DataType in GridFieldTypeSet) then Exit; //§PÂ_¦r¬qÃþ«¬

if Column.Field.FieldKind = fkLookup then
cFieldName := Column.Field.KeyFields
else if Column.Field.FieldKind = fkCalculated then
cFieldName := Column.Field.KeyFields
else
cFieldName := Column.FieldName;
//=================================AdoDataSet=====================

if DataSet is TCustomADODataSet then
begin
s := TCustomADODataSet(DataSet).Sort;
if s = '' then
begin
s := cFieldName;
Column.Title.Caption := '¡¶' + Column.Title.Caption;
end
else
begin
if Pos(cFieldName, s) <> 0 then
begin
i := Pos('DESC', s);
if i <= 0 then
begin
s := s + ' DESC';
Column.Title.Caption := '¡¿' + Column.Title.Caption;
end
else
begin
Column.Title.Caption := '¡¶' + Column.Title.Caption;
Delete(s, i, 4);
end;
end
else
begin
s := cFieldName;
Column.Title.Caption := '¡¶' + Column.Title.Caption;
end;
end;
TCustomADODataSet(DataSet).Sort := s;
end
//============================Clientdataset==========================
else if DataSet is TClientDataSet then
begin
if TClientDataSet(DataSet).indexfieldnames <> '' then
begin
i := TClientDataSet(DataSet).IndexDefs.IndexOf('i' + Column.FieldName);
if i = -1 then
begin
with TClientDataSet(DataSet).IndexDefs.AddIndexDef do
begin
Name := 'i' + Column.FieldName;
Fields := Column.FieldName;
DescFields := Column.FieldName;
end;
end;
TClientDataSet(DataSet).IndexFieldNames := '';
TClientDataSet(DataSet).IndexName := 'i' + Column.FieldName;
Column.Title.Caption := '¡¿' + Column.Title.Caption;
end
else
begin
TClientDataSet(DataSet).IndexName := '';
TClientDataSet(DataSet).IndexFieldNames := column.fieldname;
Column.Title.Caption := '¡¶' + Column.Title.Caption;
end;
end;
end;
//======================================================================


procedure WWGridTitleSort(Grid: TwwDBGrid; TitleName: String);
Type
TFieldTypeSet = set of TFieldType;
var
s, cFieldName: string;
i: integer;
DataSet: TDataSet;
aField: TField;
GridFieldTypeSet: TFieldTypeSet;
procedure SetTitle;
var
ii: integer;
cStr: string;
c: TwwColumn;
begin
for ii := 0 to Grid.DataSource.DataSet.FieldCount -1 do
begin
c := Grid.Columns[ii];
cStr := c.DisplayLabel;
if (pos('¡¶', cStr) = 1) or (pos('¡¿', cStr) = 1) then
begin
Delete(cStr, 1, 2);
c.DisplayLabel := cStr;
end;
end;
end;
begin
SetTitle;
DataSet := Grid.DataSource.DataSet;
aField := DataSet.FieldByName(TitleName);

GridFieldTypeSet := [ftString, ftSmallint, ftInteger, ftWord,
ftBoolean, ftFloat, ftCurrency, ftBCD, ftDate,
ftTime, ftDateTime, ftBytes, ftVarBytes,
ftTypedBinary, ftFixedChar, ftWideString,
ftLargeint, ftVariant];
if not (aField.DataType in GridFieldTypeSet) then Exit; //§PÂ_¦r¬qÃþ«¬

if aField.FieldKind = fkLookup then
cFieldName := aField.KeyFields
else if aField.FieldKind = fkCalculated then
cFieldName := aField.KeyFields
else
cFieldName := aField.FieldName;
//=================================AdoDataSet=====================

if DataSet is TCustomADODataSet then
begin
s := TCustomADODataSet(DataSet).Sort;
if s = '' then
begin
s := cFieldName;
aField.DisplayLabel := '¡¶' + aField.DisplayLabel;
end
else
begin
if Pos(cFieldName, s) <> 0 then
begin
i := Pos('DESC', s);
if i <= 0 then
begin
s := s + ' DESC';
aField.DisplayLabel := '¡¿' + aField.DisplayLabel;
end
else
begin
aField.DisplayLabel := '¡¶' + aField.DisplayLabel;
Delete(s, i, 4);
end;
end
else
begin
s := cFieldName;
aField.DisplayLabel := '¡¶' + aField.DisplayLabel;
end;
end;
TCustomADODataSet(DataSet).Sort := s;
end
//============================Clientdataset==========================
else if DataSet is TClientDataSet then
begin
if TClientDataSet(DataSet).indexfieldnames <> '' then
begin
i := TClientDataSet(DataSet).IndexDefs.IndexOf('i' + aField.FieldName);
if i = -1 then
begin
with TClientDataSet(DataSet).IndexDefs.AddIndexDef do
begin
Name := 'i' + aField.FieldName;
Fields := aField.FieldName;
DescFields := aField.FieldName;
end;
end;
TClientDataSet(DataSet).IndexFieldNames := '';
TClientDataSet(DataSet).IndexName := 'i' + aField.FieldName;
aField.DisplayLabel := '¡¿' + aField.DisplayLabel;
end
else
begin
TClientDataSet(DataSet).IndexName := '';
TClientDataSet(DataSet).IndexFieldNames := aField.FieldName;
aField.DisplayLabel := '¡¶' + aField.DisplayLabel;
end;
end;
end;
//======================================================================
 
to bluedog:怎么有烂码?
 
后退
顶部