1.先设你要排序列的Title->TitleButton属性为true; 2.再在DBGridEh的OnTitleBtnClick中写入以下代码: dgeStore: TDBGridEh; ... procedure TfrmEhlib.dgeStoreTitleBtnClick(Sender: TObject; ACol: Integer; Column: TColumnEh); begin if column.Title.SortMarker = smUpEh then begin aqStore.Sort := column.FieldName + ' DESC'; column.Title.SortMarker := smDownEh; end else begin aqStore.Sort := column.FieldName + ' ASC'; column.Title.SortMarker := smUpEh; end; end;
to weizi2000(秋风啊) 1.先设你要排序列的Title->TitleButton属性为true; “Title->TitleButton”在哪设置为什么我没找到?? 我的代码: procedure TForm1.DBGrid1TitleClick(Column: TColumn); begin ADOQuery1.Close; ADOQuery1.SQL.Clear; ADOQuery1.SQL.Text:= 'select * from MainTable order by'+"'+ Column.FieldName +'''; ADOQuery1.Open; end; 为什么不管单击那里都是对第一列进行排列??
to weizi2000(秋风啊) aqStore 是什么???如果是DBGrideh 没有找到Sort这个属性啊!
这么写也不对啊 DBGRidEh1.DataSource.DataSet.sort
dbgrideh的属性面板 OptionsEh -〉dghAutoSortMarking 设为ture双击dbgrideh 右键add all fields 选中一个,找属性title->titlebutton 按weizi2000(秋风啊)的
楼上说的我都作了啊!!! 可编译的时候说aqStore没有定义。不知道是哪来的啊。
看秋风的上下文,这个aqStore是一个数据集,应该是个adoquery之类的
麻烦楼上大哥作个例子试试。数据集没有Sort属性啊。
本地数据集没有sort属性,ADO有;dghAutoSortMarking 不用设为ture
如果我用的是BDE连接那就要舍弃这个功能吗?本地数据库的应该怎么实现此功能呢?
来个delphi自己的DBGrid,用DBX,自己改一下 procedure runonclicktitlesort(Column: TColumn); var i:integer; s1:string; ACld:TClientDataSet; begin if Column.Field=nil then exit; ACld:=TClientDataSet(Column.Field.DataSet); s1:=Column.Title.Caption; if (pos('▲',s1)=1) or (pos('▼',s1)=1) then begin Delete(s1,1,2); Column.Title.Caption:=s1; end; if ACld.indexfieldnames<>'' then//有排序列 begin i:=ACld.IndexDefs.IndexOf('i'+Column.FieldName); if i=-1 then begin with ACld.IndexDefs.AddIndexDef do begin Name:='i'+Column.FieldName; Fields:=Column.FieldName; DescFields:=Column.FieldName; end; end; ACld.IndexFieldNames:=''; ACld.IndexName:='i'+Column.FieldName; Column.Title.Caption:='▼'+Column.Title.Caption; end else//没有索引,用IndexFieldNames begin ACld.IndexName:=''; ACld.IndexFieldNames:=column.fieldname; Column.Title.Caption:='▲'+Column.Title.Caption; end; end;
2.再在DBGridEh的OnTitleBtnClick中写入以下代码:
dgeStore: TDBGridEh;
...
procedure TfrmEhlib.dgeStoreTitleBtnClick(Sender: TObject; ACol: Integer;
Column: TColumnEh);
begin
if column.Title.SortMarker = smUpEh then
begin
aqStore.Sort := column.FieldName + ' DESC';
column.Title.SortMarker := smDownEh;
end
else
begin
aqStore.Sort := column.FieldName + ' ASC';
column.Title.SortMarker := smUpEh;
end;
end;
1.先设你要排序列的Title->TitleButton属性为true;
“Title->TitleButton”在哪设置为什么我没找到??
我的代码:
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
begin
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Text:= 'select * from MainTable order by'+"'+ Column.FieldName +''';
ADOQuery1.Open;
end;
为什么不管单击那里都是对第一列进行排列??
aqStore 是什么???如果是DBGrideh
没有找到Sort这个属性啊!
DBGRidEh1.DataSource.DataSet.sort
OptionsEh -〉dghAutoSortMarking 设为ture双击dbgrideh 右键add all fields
选中一个,找属性title->titlebutton
按weizi2000(秋风啊)的
可编译的时候说aqStore没有定义。不知道是哪来的啊。
procedure runonclicktitlesort(Column: TColumn);
var
i:integer;
s1:string;
ACld:TClientDataSet;
begin
if Column.Field=nil then exit;
ACld:=TClientDataSet(Column.Field.DataSet);
s1:=Column.Title.Caption;
if (pos('▲',s1)=1) or (pos('▼',s1)=1) then
begin
Delete(s1,1,2);
Column.Title.Caption:=s1;
end;
if ACld.indexfieldnames<>'' then//有排序列
begin
i:=ACld.IndexDefs.IndexOf('i'+Column.FieldName);
if i=-1 then
begin
with ACld.IndexDefs.AddIndexDef do
begin
Name:='i'+Column.FieldName;
Fields:=Column.FieldName;
DescFields:=Column.FieldName;
end;
end;
ACld.IndexFieldNames:='';
ACld.IndexName:='i'+Column.FieldName;
Column.Title.Caption:='▼'+Column.Title.Caption;
end
else//没有索引,用IndexFieldNames
begin
ACld.IndexName:='';
ACld.IndexFieldNames:=column.fieldname;
Column.Title.Caption:='▲'+Column.Title.Caption;
end;
end;