DBGRID控件(100分)

  • 主题发起人 主题发起人 DGGLEN
  • 开始时间 开始时间
D

DGGLEN

Unregistered / Unconfirmed
GUEST, unregistred user!
在DBGRID控件中点击表头的字段后让其进行排序,就如OUTLOOK EXPRESS那样,一下是升序,再点一下是降序,请问这样能做到吗?谢谢!
 
用DBGRIDEH控件可以实现。还可以多表头,合计行等功能。
 
DBGRID是做不到的,你要自己写代码。或者使用别的控件,我用的是AdvStringGrid
 
表头排序,要先设置属性,再在事件titleclick中写代码控制
 
试试下面的代码:<br>&nbsp; procedure DBGrid1TitleSort(Column:TColumn);<br>&nbsp; &nbsp; var<br>&nbsp; &nbsp; &nbsp; &nbsp;s,cFieldName:string;<br>&nbsp; &nbsp; &nbsp; &nbsp;SqlStr,TempStr: string;<br>&nbsp; &nbsp; &nbsp; &nbsp;i:integer;<br>&nbsp; &nbsp; &nbsp; &nbsp;DataSet:TDataSet;<br>&nbsp; &nbsp; &nbsp;procedure setTitle;<br>&nbsp; &nbsp; &nbsp; &nbsp;var ii:integer;cStr:string;c:TColumn;<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for ii:=0 to TDBGrid(Column.Grid).Columns.Count-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c:=TDBGrid(Column.Grid).Columns[ii];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cStr:=c.Title.Caption;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (pos('▲',cStr)=1) or (pos('▼',cStr)=1) then begin Delete(cStr,1,2); c.Title.Caption:=cStr; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; setTitle;<br>&nbsp; &nbsp; &nbsp; DataSet:=Column.Grid.DataSource.DataSet;<br>&nbsp; &nbsp; &nbsp; if Column.Field.FieldKind=fkLookup then cFieldName:=Column.Field.KeyFields<br>&nbsp; &nbsp; &nbsp; &nbsp; else if Column.Field.FieldKind=fkCalculated then cFieldName:=Column.Field.KeyFields<br>&nbsp; &nbsp; &nbsp; &nbsp; else cFieldName:=Column.FieldName;<br>&nbsp; &nbsp; &nbsp; ////=================================AdoDataSet=====================<br>&nbsp; &nbsp; &nbsp; if DataSet is TCustomADODataSet then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; s:=TCustomADODataSet(DataSet).Sort;<br>&nbsp; &nbsp; &nbsp; &nbsp; if s='' then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin s:=cFieldName; Column.Title.Caption:='▲'+Column.Title.Caption;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Pos(cFieldName,s)&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i:=Pos('DESC',s);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if i&lt;=0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin s:=s+' DESC'; Column.Title.Caption:='▼'+Column.Title.Caption;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin Column.Title.Caption:='▲'+Column.Title.Caption; Delete(s,i,4); end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin s:=cFieldName; Column.Title.Caption:='▲'+Column.Title.Caption; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; TCustomADODataSet(DataSet).Sort:=s; &nbsp;/////=================================AdoDataSet==================<br>&nbsp; &nbsp; &nbsp; end else if (DataSet is TQuery) or (DataSet is TTable) then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; while Pos(cFieldName,';')&lt;&gt;0 do cFieldName:=copy(cFieldName,1,Pos(cFieldName,';')-1)+ ','+ copy(cFieldName,Pos(cFieldName,';')+1,100);<br>&nbsp; &nbsp; &nbsp; with TQuery(TDBGrid(Column.Grid).DataSource.DataSet) do<br>&nbsp; &nbsp; &nbsp; &nbsp; begin &nbsp;////=================================BDEQuery=====================<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SqlStr:=Trim(UpperCase(Sql.Text));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if RightStr(SqlStr,1)=';' then begin SqlStr:=LeftStr(SqlStr,length(Sqlstr)-1); &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if pos(cFieldName,SqlStr)=0 then exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (pos('ORDER',SqlStr)=0) or (pos('ASC',SqlStr)=0) then begin TempStr:=' Order By '+cFieldName+' Asc'; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else begin &nbsp; TempStr:= ' Order By '+cFieldName+' Desc'; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if pos('ORDER',SqlStr)&lt;&gt;0 then SqlStr:=Copy(SqlStr,1,pos('ORDER',SqlStr)-1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SqlStr:=SqlStr+TempStr+';';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Active:=False;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sql.Clear;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sql.Text:=SqlStr;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Prepare;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Open;<br>&nbsp; &nbsp; &nbsp; &nbsp; end; &nbsp; ////=================================BDEQuery=====================<br>&nbsp; &nbsp; &nbsp; end else if DataSet is TClientDataSet then &nbsp;/////////============================Clientdataset==================<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if TClientDataSet(DataSet).indexfieldnames&lt;&gt;'' then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i:=TClientDataSet(DataSet).IndexDefs.IndexOf('i'+Column.FieldName);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if i=-1 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with TClientDataSet(DataSet).IndexDefs.AddIndexDef do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name:='i'+Column.FieldName;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Fields:=Column.FieldName;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DescFields:=Column.FieldName;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TClientDataSet(DataSet).IndexFieldNames:='';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TClientDataSet(DataSet).IndexName:='i'+Column.FieldName;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Column.Title.Caption:='▼'+Column.Title.Caption;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TClientDataSet(DataSet).IndexName:='';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TClientDataSet(DataSet).IndexFieldNames:=column.fieldname;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Column.Title.Caption:='▲'+Column.Title.Caption;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; /////////============================Clientdataset======================<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; end;
 
必须自己写代码控制,楼上的可以参考~
 
楼上的方法太苯了吧~!看我的...<br>procedure DataSort(Column: TField; DataSet:TDataSet);<br>var<br>&nbsp; str:string;<br>begin<br>&nbsp; if DataSet.IsEmpty then exit;<br>&nbsp; if Column.DataType in[ftMemo, ftGraphic, ftFmtMemo] then exit;<br>&nbsp; str:=Trim(TADODataSet(DataSet).Sort);<br>&nbsp; if (str='')or(Copy(str,Length(str)-3,4)='DESC') then<br>&nbsp; &nbsp; TADODataSet(DataSet).Sort:=Column.FieldName+' ASC'<br>&nbsp; else<br>&nbsp; &nbsp; TADODataSet(DataSet).Sort:=Column.FieldName+' DESC';<br>end;<br><br>procedure TForm1.DBGrid1TitleClick(Column: TColumn);<br>begin<br>&nbsp; DataSort(Column.Field,DBGrid1.DataSource.DataSet);<br>end;
 
谢谢"菜鸟黄",你这个方法太好用了,再次非常感谢!
 
后退
顶部