试试下面的代码:<br> procedure DBGrid1TitleSort(Column:TColumn);<br> var<br> s,cFieldName:string;<br> SqlStr,TempStr: string;<br> i:integer;<br> DataSet:TDataSet;<br> procedure setTitle;<br> var ii:integer;cStr:string;c:TColumn;<br> begin<br> for ii:=0 to TDBGrid(Column.Grid).Columns.Count-1 do<br> begin<br> c:=TDBGrid(Column.Grid).Columns[ii];<br> cStr:=c.Title.Caption;<br> if (pos('▲',cStr)=1) or (pos('▼',cStr)=1) then begin Delete(cStr,1,2); c.Title.Caption:=cStr; end;<br> end;<br> end;<br> begin<br> setTitle;<br> DataSet:=Column.Grid.DataSource.DataSet;<br> if Column.Field.FieldKind=fkLookup then cFieldName:=Column.Field.KeyFields<br> else if Column.Field.FieldKind=fkCalculated then cFieldName:=Column.Field.KeyFields<br> else cFieldName:=Column.FieldName;<br> ////=================================AdoDataSet=====================<br> if DataSet is TCustomADODataSet then<br> begin<br> s:=TCustomADODataSet(DataSet).Sort;<br> if s='' then<br> begin s:=cFieldName; Column.Title.Caption:='▲'+Column.Title.Caption;<br> end else<br> begin<br> if Pos(cFieldName,s)<>0 then<br> begin<br> i:=Pos('DESC',s);<br> if i<=0 then<br> begin s:=s+' DESC'; Column.Title.Caption:='▼'+Column.Title.Caption;<br> end else<br> begin Column.Title.Caption:='▲'+Column.Title.Caption; Delete(s,i,4); end;<br> end else<br> begin s:=cFieldName; Column.Title.Caption:='▲'+Column.Title.Caption; end;<br> end;<br> TCustomADODataSet(DataSet).Sort:=s; /////=================================AdoDataSet==================<br> end else if (DataSet is TQuery) or (DataSet is TTable) then<br> begin<br> while Pos(cFieldName,';')<>0 do cFieldName:=copy(cFieldName,1,Pos(cFieldName,';')-1)+ ','+ copy(cFieldName,Pos(cFieldName,';')+1,100);<br> with TQuery(TDBGrid(Column.Grid).DataSource.DataSet) do<br> begin ////=================================BDEQuery=====================<br> SqlStr:=Trim(UpperCase(Sql.Text));<br> if RightStr(SqlStr,1)=';' then begin SqlStr:=LeftStr(SqlStr,length(Sqlstr)-1); end;<br> //if pos(cFieldName,SqlStr)=0 then exit;<br> if (pos('ORDER',SqlStr)=0) or (pos('ASC',SqlStr)=0) then begin TempStr:=' Order By '+cFieldName+' Asc'; end<br> else begin TempStr:= ' Order By '+cFieldName+' Desc'; end;<br> if pos('ORDER',SqlStr)<>0 then SqlStr:=Copy(SqlStr,1,pos('ORDER',SqlStr)-1);<br> SqlStr:=SqlStr+TempStr+';';<br> Active:=False;<br> Sql.Clear;<br> Sql.Text:=SqlStr;<br> Prepare;<br> Open;<br> end; ////=================================BDEQuery=====================<br> end else if DataSet is TClientDataSet then /////////============================Clientdataset==================<br> begin<br> if TClientDataSet(DataSet).indexfieldnames<>'' then<br> begin<br> i:=TClientDataSet(DataSet).IndexDefs.IndexOf('i'+Column.FieldName);<br> if i=-1 then<br> begin<br> with TClientDataSet(DataSet).IndexDefs.AddIndexDef do<br> begin<br> Name:='i'+Column.FieldName;<br> Fields:=Column.FieldName;<br> DescFields:=Column.FieldName;<br> end;<br> end;<br> TClientDataSet(DataSet).IndexFieldNames:='';<br> TClientDataSet(DataSet).IndexName:='i'+Column.FieldName;<br> Column.Title.Caption:='▼'+Column.Title.Caption;<br> end else<br> begin<br> TClientDataSet(DataSet).IndexName:='';<br> TClientDataSet(DataSet).IndexFieldNames:=column.fieldname;<br> Column.Title.Caption:='▲'+Column.Title.Caption;<br> end; /////////============================Clientdataset======================<br> end;<br> end;