請問SMDBGrid的排序怎麼用? |
尚未結案
|
shougo
一般會員 發表:5 回覆:4 積分:1 註冊:2003-03-20 發送簡訊給我 |
我參考了線上的FAQ,也只有設定Sort Field,我試過了在onTitleClick裡寫上
With SMDBGrid Do
Begin
(Columns[2] as TSMDBColumn).SortType := stAscending;
End;
也試過下面這種方法
SMDBGrid.SetSortField(DMDss.QryReport.FindField('xxx'), stAscending);
但都還是無法按了Title就出現我要的排序結果,請問我是不是還漏了什麼未寫呢?請幫幫我,感謝!!
|
wwwbbs
初階會員 發表:41 回覆:59 積分:25 註冊:2003-05-23 發送簡訊給我 |
您試看看,請在SMDBGrid的OnTitleClick事件撰寫如下Code,我是用ADOQuery...這也是在K.Top跟前輩學來的,但素忘了是哪一篇文章,所以只好在此借花現佛一下囉!!
[code delphi] procedure Tfm201POClient.SMDBGrid1TitleClick(Column: TColumn); begin (Column.Grid as TSMDBGrid).ClearSort; if RightStr(ADOQuery1.Sort,3)='ESC' THEN begin ADOQuery1.Sort:=Column.FieldName ' ASC'; TSMDBColumn(Column).SortType := stAscending; end ELSE begin ADOQuery1.Sort:=Column.FieldName ' DESC'; TSMDBColumn(Column).SortType := stDescending; end; end; [/code] ===================引 用 shougo 文 章=================== 我參考了線上的FAQ,也只有設定Sort Field,我試過了在onTitleClick裡寫上 With SMDBGrid Do Begin (Columns[2] as TSMDBColumn).SortType := stAscending; End; 也試過下面這種方法 SMDBGrid.SetSortField(DMDss.QryReport.FindField('xxx'), stAscending); 但都還是無法按了Title就出現我要的排序結果,請問我是不是還漏了什麼未寫呢?請幫幫我,感謝!! |
stacker_liew
中階會員 發表:59 回覆:168 積分:65 註冊:2004-05-17 發送簡訊給我 |
|
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
參考: http://www.hadp.org/downloads/sortclientdst.htm
Sorting the Contents of a ClientDataSet From Introducing Kylix with Cary Jensen I promised (HADP) a code segment that sorted the contents of a ClientDataSet in response to clicking the column headers of a DBGrid. Here it is... -Cary //By Cary Jensen //For demonstration purposes only. By using this code you //agree to hold its author free from any claim arising out //of its use or misuse. //Use the TypeInf unit uses TypInfo; //TCustomClientDataSet is not available in Delphi 5. Changed to TClientDataSet function SortCustomClientDataSet(DataSet: TClientDataSet; const FieldName: String): Boolean; var i: Integer; IndexDefs: TIndexDefs; IndexName: String; IndexOptions: TIndexOptions; begin Result := False; if IsPublishedProp(DataSet, 'IndexDefs') then IndexDefs := GetObjectProp(DataSet, 'IndexDefs') as TIndexDefs else Exit; if IsPublishedProp(DataSet, 'IndexName') then IndexName := GetStrProp(DataSet, 'IndexName') else Exit; IndexDefs.Update; if DataSet.Fields.FindField(FieldName) = nil then Exit; if IndexName = FieldName '__IdxA' then begin IndexName := FieldName '__IdxD'; IndexOptions := [ixDescending]; end else begin IndexName := FieldName '__IdxA'; IndexOptions := []; end; for i := 0 to Pred(IndexDefs.Count) do begin if IndexDefs[i].Name = IndexName then begin Result := True; Break end; //if end; // for if not Result then begin DataSet.AddIndex(IndexName, FieldName,IndexOptions); Result := True; end; // if not SetStrProp(DataSet, 'IndexName', IndexName); end; //Call SortCustomClientDataSet from a DBGrid's OnTitleClick event handler procedure TForm1.DBGrid1TitleClick(Column: TColumn); begin SortCustomClientDataset(ClientDataSet1,Column.FieldName); end; |
stacker_liew
中階會員 發表:59 回覆:168 積分:65 註冊:2004-05-17 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |