cxGrid用法-最新
cxGrid用法步
1.你先放一個cxGrid,設置好View,設置View.DataController連接的DataSource 2.激活DataSource連接的DataSet,雙擊cxGrid,點擊Retrieve Fields,取得所有的Column 3.設置View的OptionsView.Footer=True,OptionsView.GroupFooters=True,這是為了把分組小計和總計面板顯示出來 4.將“訂單號”字段拖到cxGrid上方的分組面板(GroupbyBox),將數據按“訂單號”分組。這時你會發現單身所有的數據都縮起來了,如果想使所有的數據都展開,可以設置View.DataController.Options.dcoGroupsAlwaysExpanded=True 5.設置分組小計:把View.DataController.Summary.DefaultGroupSummaryItems點開,新增一個Item,Column屬性在下拉裏選擇“數量”字段,FieldName屬性為空,Format屬性可以設置數值的顯示格式,Kind屬性下拉skSum加總,Position屬性一定要選擇spFooter。 6.設置總計:把View.DataController.Summary.FooterSummaryItems點開,新增一個Item,Column屬性在下拉裏選擇“數量”字段,FieldName屬性為空,Format屬性可以設置數值的顯示格式,Kind屬性下拉skSum加總,Position屬性一定要選擇spFooter。 大功告成,按F9看一下勝利果實吧。 再奉送一個技巧,在Form1再放一個TcxGridPopupMenu控件,就在cxGrid控件旁邊的那個,把TcxGridPopupMenu的Grid屬性設置成你的cxGrid。 然後運行程序,在運行狀態,點擊Grid上的所有地方,左鍵或右鍵,你都會有意外收獲。
(1)動態設置顯示格式 procedure SetDisplayFormat(ACtrlData: TClientDataSet; TbView: TcxGridDBTableView); var i: integer; begin if ACtrlData.RecordCount <= 0 then Exit; try TbView.ClearItems; ACtrlData.First; for i := 0 to ACtrlData.RecordCount - 1 do begin if ACtrlData.FieldByName(‘SQBF_DisplayInGrid‘).AsString = ‘1‘ then //在表格中顯示 with TbView.CreateColumn do begin DataBinding.FieldName := ACtrlData.FieldByName(‘SQBF_FieldName‘).AsString; Caption := ACtrlData.FieldByName(‘SQBF_Caption‘).AsString; //字段中文標題 Hint := ACtrlData.FieldByName(‘SQBF_Hint‘).AsString; Width := ACtrlData.FieldByName(‘SQBF_Width‘).AsInteger; HeaderAlignmentHorz := taCenter; end; ACtrlData.Next; end; except on E: Exception do SaveLog(‘設置顯示格式時出錯:‘ + E.Message); end; end; (2)顯示行號 procedure TFmQueryBase.cxDBViewMasterCustomDrawIndicatorCell( Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean); var FValue: string; FBounds: TRect; begin FBounds := AViewInfo.Bounds; if (AViewInfo is TcxGridIndicatorRowItemViewInfo) then begin ACanvas.FillRect(FBounds); ACanvas.DrawComplexFrame(FBounds, clBlack, clBlack, [bBottom, bLeft, bRight], 1); FValue := IntToStr(TcxGridIndicatorRowItemViewInfo(AViewInfo).GridRecord.Index+1); InflateRect(FBounds, -3, -2); //Platform specific. May not work on Linux. ACanvas.Font.Color := clBlack; ACanvas.Brush.Style := bsClear; ACanvas.DrawText(FValue, FBounds, cxAlignCenter or cxAlignTop); ADone := True; end; end; (3)設置顯示格式,我的項目要求先動態添加字段,這時不知道字段類型,所以設置DisplayFormat不方便,我還沒有找到好方法。 所以采用打開數據集後再設置: procedure TFmQueryBase.cdsMasterAfterOpen(DataSet: TDataSet); var i: Integer; begin for i := 0 to cxDBViewMaster.DataController.DataSet.FieldCount -1 do begin if cxDBViewMaster.DataController.DataSet.Fields[i] is TNumericField then begin if Pos(‘AMOUNT‘, UpperCase(cxDBViewMaster.DataController.DataSet.Fields[i].FieldName)) > 0 then begin TNumericField(cxDBViewMaster.DataController.DataSet.Fields[i]).DisplayFormat := ‘#,##0.000‘; Continue; end; if Pos(‘QUANTITY‘, UpperCase(cxDBViewMaster.DataController.DataSet.Fields[i].FieldName)) > 0 then begin TNumericField(cxDBViewMaster.DataController.DataSet.Fields[i]).DisplayFormat := ‘#,##0.000‘; Continue; end; if Pos(‘MONEY‘, UpperCase(cxDBViewMaster.DataController.DataSet.Fields[i].FieldName)) > 0 then begin TNumericField(cxDBViewMaster.DataController.DataSet.Fields[i]).DisplayFormat := ‘#,##0.00‘; Continue; end; end; end; end; 2007-7-19 12:48:54 查看評語»»» 2007-7-19 12:53:09 別人的,轉載http://www.showding.cn/item/cxGrid__182526.aspx 最近在學習使用cxGrid,安裝的版本是ExpressQuantumGrid Suite v5.10 我發現這個控件功能雖然強大,但是非常難用。 現在我手頭就有幾個問題還沒解決: 1)主從模式下導出Excel中文會產生亂碼,而且從表內容沒有導出。 我不知道是不是因為我的字段名包括單引號的原因。 導出代碼:ExportGrid4ToExcel(FileName, cxGrid); 2)主從模式下通過按鈕對從表添加/刪除行,代碼怎麽寫。 附:單表添加/刪除行的代碼 procedure TFormAccount.cxButtonNewClick(Sender: TObject); begin Self.tvAccount.DataController.Append; Self.tvAccount.Columns[0].Focused := True; cxGrid.SetFocus; end; procedure TFormAccount.cxButtonDeleteClick(Sender: TObject); begin if Self.tvAccount.DataController.RowCount = 0 then Exit; if Application.MessageBox(‘確認刪除當前記錄?‘, ‘確認刪除‘, MB_YesNo + MB_IconQuestion) = IDNO then Exit; Self.tvAccount.DataController.DeleteFocused; end; 3)動態創建主從結構出錯(Compiler沒錯,運行時出現系統錯誤0000000018), 我使用了二個ADOStoreProcedure作主從表 代碼如下: var Level: TcxGridLevel; GridView: TcxGridDBTableView; begin Level := cxGrid1.Levels[0].Add; GridView := TcxGridDBTableView(cxGrid1.CreateView(TcxGridDBTableView)); GridView.DataController.DataSource := Self.dsDetail; GridView.DataController.KeyFieldNames := ‘PurchOrderID;POLineNbr;PromiseDate;ReceiverDate‘; GridView.DataController.MasterKeyFieldNames := ‘VendorID‘; GridView.DataController.DetailKeyFieldNames := ‘VendorID‘; GridView.DataController.DataModeController.SmartRefresh := True; GridView.OptionsCustomize.ColumnHiding := True; GridView.OptionsCustomize.ColumnsQuickCustomization := True; GridView.OptionsData.Deleting := False; GridView.OptionsData.Inserting := False; GridView.OptionsView.Indicator := True; Level.GridView := GridView; GridView := TcxGridDBTableView(cxGrid1.Levels[0].GridView); GridView.DataController.KeyFieldNames := ‘VendorID‘; GridView.OptionsView.GroupByBox := False; //顯示主表內容 tvResult.BeginUpdate; tvResult.ClearItems; tvResult.DataController.CreateAllItems; tvResult.EndUpdate; //顯示明細表內容 GridView := TcxGridDBTableView(cxGrid1.Levels[0].Items[0].GridView); GridView.BeginUpdate; GridView.ClearItems; GridView.DataController.CreateAllItems; GridView.DataController.Refresh; GridView.EndUpdate; end; 此樓回復Re: -------------------------------------------------------------------------------- 小技巧:用代碼展開/收縮主從結構 Self.tvDepartment.ViewData.Expand(True); Self.tvDepartment.ViewData.Collaspe(True); 註:tvDepartment為主表對應的TableView 此樓回復Re: -------------------------------------------------------------------------------- 你說的這幾個問題我也遇到過。 第一個問題是編碼的問題,修改了其中關於編碼的函數,OK. 第二個問題在cxGrid的社區可以找到解答,但從表必須滿足某種條件,例如關鍵字排序。 第三個問題的解決辦法,你可以嘗試在動態創建的代碼前後加上: grid.beginupdate; ... grid.endupdate 來解決。 此樓回復Re: -------------------------------------------------------------------------------- 沒用過 不要經常使用三方控件 此樓回復Re: -------------------------------------------------------------------------------- to tttk(網絡芝麻): 第一個問題:如何修改啊,貼出代碼 第二個問題:沒搜到啊 第三個問題:試一下再說 此樓回復Re: -------------------------------------------------------------------------------- 不要經常使用三方控件 ====================== 我感覺不用cxGrid的話,沒必要用Delphi了,呵呵 此樓回復Re: -------------------------------------------------------------------------------- 樓上這話是不是有點問題?DELPHI能做得事情很多很多,難道非要用CXGRID?CXGRID不是用DELPHI做出來得? 此樓回復Re: -------------------------------------------------------------------------------- 沒用過..... 此樓回復Re: -------------------------------------------------------------------------------- 回復人: zxkid(沒有人會像我這樣...) ( ) 信譽:101 2006-01-06 16:58:00 得分: 0 不要經常使用三方控件 ====================== 我感覺不用cxGrid的話,沒必要用Delphi了,呵呵 ********** 樓主乃天人也!! 此樓回復Re: -------------------------------------------------------------------------------- 呵呵 此樓回復Re: -------------------------------------------------------------------------------- cxGrid比較不錯,我也使用過導出到Excel,沒有遇到你說的亂碼 主從表也沒有問題的,其實跟單表操作還不是一回事 此樓回復Re: -------------------------------------------------------------------------------- up 此樓回復Re: -------------------------------------------------------------------------------- 沒用過cxGrid,以後考慮 此樓回復Re: -------------------------------------------------------------------------------- 樓主乃天人也!! ============================= Delphi下有cxGrid, .NET下有XtraGrid, 它們都是同一公司出的。 遲早都會轉到.NET,所以。。。 此樓回復Re: -------------------------------------------------------------------------------- 路過 此樓回復Re: -------------------------------------------------------------------------------- 用過,挺好,只會使用最簡單的。 此樓回復Re: -------------------------------------------------------------------------------- 發一個郵件給我,我把解決亂碼後的源代碼發一分給你,放到你的項目文件夾下即可。 [email protected]
cxGrid用法-最新