cxGrid 在 GridMode = True 模式下实现标题点击排序以及标题列过滤筛选!!!
最近使用 cxGrid 這個(gè)表格控件,發(fā)現(xiàn)加載十幾萬(wàn)筆數(shù)據(jù)時(shí)加載特別慢,同時(shí)用 DBGridEh 加載來(lái)對(duì)比,發(fā)現(xiàn)速度差異很大,
原來(lái)是 cxGrid 的 GridView 默認(rèn) GridMode=False ,此時(shí)加載數(shù)據(jù)集時(shí)會(huì)設(shè)置每一列的屬性(例如把每一列的不同值轉(zhuǎn)入篩選器中),
所以速度會(huì)慢很多,所以把 GridMode=True 后,速度和 DBGridEh 差不多了,但是問(wèn)題來(lái)了,設(shè)置這個(gè)屬性后,原本的點(diǎn)擊
標(biāo)題列功能無(wú)法排序,然后列篩序也不能使用。(要知道,這個(gè)2個(gè)功能是cxGrid 的特色,如果無(wú)法使用,那還不如使用DBGridEh)。
通過(guò)上網(wǎng)搜索了一下,基本能實(shí)現(xiàn)這2個(gè)功能,不過(guò)沒(méi)有封裝成通用的方法,還請(qǐng)大家多提出意見(jiàn),代碼如下:
1.標(biāo)題點(diǎn)擊排序功能:
procedure TForm1.cxGrid1DBBandedTableView1DataControllerSortingChanged(Sender: TObject); vari:Integer;sortstr:string; begintrysortstr:='';for i:= 0 to cxGrid1DBBandedTableView1.ColumnCount - 1 dobeginwith cxGrid1DBBandedTableView1.Columns[i] dobegincase SortOrder ofsoAscending:beginif sortstr<>'' thensortstr:=sortstr+',';sortstr:=sortstr+DataBinding.FieldName;end;soDescending:beginif sortstr<>'' thensortstr:=sortstr+',';sortstr:=sortstr+DataBinding.FieldName+' DESC';end;end;end;end;if sortstr<>'' thenad.Sort:=sortstr;finallyend; end;我用的是 adodataset 數(shù)據(jù)組件,加上以上代碼可以多列排序。注意:需要添加 ?dxCore 單元才能使用 SoDescending 等變量。
2.實(shí)現(xiàn)列篩選代碼如下:
varn,i: integer;field: string; beginfield := cxGrid1DBBandedTableView1.Columns[AItemIndex].DataBinding.FieldName;qy.close;qy.SQL.Clear;qy.SQL.Add('select distinct '+field+' from mbrecord ');qy.Open;n := qy.RecordCount;qy.First;for i := 0 to n-1 dobeginAValueList.Add(fvivalue,qy.FieldValues[field],vartostr(qy.FieldValues[field]),false);qy.Next;end; end;以上代碼是加載沒(méi)一列的篩選值列表;
?
procedure TForm1.cxGrid1DBBandedTableView1DataControllerFilterChanged(Sender: TObject); beginad.Filtered:=False;if cxGrid1DBBandedTableView1.DataController.Filter.FilterText<>'' thenbeginad.Filter:=cxGrid1DBBandedTableView1.DataController.Filter.FilterText;Tryad.Filtered:=True;FinallyEnd; end; end;以上代碼是當(dāng)選擇篩選值后,對(duì)應(yīng)要執(zhí)行的數(shù)據(jù)查詢方法;
? ?注意: 這個(gè)語(yǔ)句
ad.Filter:=cxGrid1DBBandedTableView1.DataController.Filter.FilterText;
不能直接這樣寫(xiě),需要轉(zhuǎn)移一下,因?yàn)槿绻愕臉?biāo)題列標(biāo)題是中文的話,那么這個(gè) FilterText 也是中文的名字,
所以要把這個(gè) FilterText 中的中文替換成對(duì)應(yīng)的英文字段才可以使用。而且數(shù)據(jù)篩選后,
表格 DataController 的 recordcount 總數(shù)是不會(huì)變的,除非你的 DataSet 使用的是 Open 而不是過(guò)濾方式。
這樣一來(lái),有很多地方要寫(xiě)代碼,很麻煩,如果數(shù)據(jù)量少,還是建議使用 GridMode=False 方式吧。
轉(zhuǎn)載于:https://www.cnblogs.com/lpq21314/p/6010998.html
總結(jié)
以上是生活随笔為你收集整理的cxGrid 在 GridMode = True 模式下实现标题点击排序以及标题列过滤筛选!!!的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: c语言编译器masm,汇编环境搭建 --
- 下一篇: MySQL Internals Manu