版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、XtraGrid gridview基本用法DevExpress XtraGrid的功能實在強大,剛使用的時候看到一大片屬性設置,分不清東南西北,參照demo和使用中的一些經(jīng)驗,記錄一下使用方法?,F(xiàn)在數(shù)據(jù)庫訪問都使用ORM技術了,對于DataSouce綁定以下是以IList為說明對象。 控件基本定義 DevExpress.XtraGrid.GridControl gridControl1; 1、 數(shù)據(jù)綁定(IList) DevExpress.XtraGrid.Views.Grid.GridView gridView1; IList<MyClass> list = new Bindin
2、gList<MyClass>(); /初始list list.Add(A); list.Add(B); . gridControl1.DataSource = list; 2、 在Grid上編輯數(shù)據(jù) 修改屬性gridView1.OptionsView.NewItemRowPosition,設為Top或Bottom可以在Grid上添加數(shù)據(jù)。 (在demo中原文:a record object must be inherited from the IEditableObject class if you need the ability to cancel newly added re
3、cords via the grid) 譯:如果你需要通過gird取消新建的記錄,你的記錄對象必須實現(xiàn)IEditableObject (注:在測試中,感覺不需要繼承IEditableObject,在grid編輯后也能實現(xiàn)取消。demo通過實現(xiàn)IEditableObject的BeginEdit、CancelEdit方法,數(shù)據(jù)編輯后恢復特定數(shù)據(jù)。不使用grid直接修改數(shù)據(jù),可以考慮這種恢復原數(shù)據(jù)的方法。) 3、 修改列(Column)格式 DevExpress.XtraGrid.Columns.GridColumn col = gridView1.Columns0; 數(shù)據(jù)對齊方式 col.Appe
4、aranceCell.TextOptions.HAlignment, 默認值Default,可選值Default/Near/Center/Far。 說明:以下情況是基于從左到右的文字排列;若是從右到左,用法相反。 Default:數(shù)據(jù)默認的對齊方式 Near:左對齊 Center:居中對齊 Far:右對齊 列標題 col.Caption 對應綁定數(shù)據(jù)的屬性 col.FieldName 排列順序 col.VisibleIndex 格式化顯示數(shù)據(jù) Col.DisplayFormat.FormatType Col.DisplayFormat.Format Col.DisplayFormat.Form
5、atString 區(qū)別:FormatType/FormatString 使用當前系統(tǒng)的語言區(qū)域設置,F(xiàn)ormat使用特定的System.IFormatProvider設置。 (原文注釋:Use the FormatType and FormatString properties to format values based on the current language and regional settings (culture). FormatType specifies the type of values to be formatted. FormatString specifies
6、a format pattern appropriate for the current FormatType value. You can refer to the Standard Numeric Format Strings and Date and Time Format Strings topics in MSDN for information on format specifiers. Setting the FormatType property changes the format provider used when formatting values by the Get
7、DisplayText function. Format providers supply mation such as the character to use as the decimal point when formatting numeric strings and the separation character to use when formatting a System.DateTime object. The Format property specifies the format provider to use. You can change the Format pro
8、perty explicitly by assigning a System.IFormatProvider object. This can be useful if you wish to format values according to a specific culture (not the current one). In this case, you also need to set the FormatType property to FormatType.Custom.) 4、 使用Grid內(nèi)置導航欄 gridControl1.UseEmbeddedNativgator=Tr
9、ue 設定內(nèi)置導航欄按鈕其他屬性 gridControl1.EmbeddedNavigator 5、 GridView內(nèi)置方式編輯數(shù)據(jù) 禁止編輯數(shù)據(jù) gridView1.OptionsBehavior.Editable = False,默認是True 可編輯。 Gridview內(nèi)置數(shù)據(jù)編輯器顯示方式 gridView1.OptionsBehavior.EditorShowMode,可選值Default/ MouseDown/MouseUp/ Click。 說明: Default 多選Cell相當于Click,單選Cell相當于MouseDown MouseDown 在單元格內(nèi)按下鼠標鍵時打開內(nèi)
10、置編輯器 MouseUp 在單元格內(nèi)釋放鼠標鍵時打開內(nèi)置編輯器 Click 在不是編輯狀態(tài),但獲得焦點的單元格中點擊時打開編輯器。點擊非焦點單元格時,首先會切換焦點,再點擊時才打開編輯器 6、 設定GrideView單元格的內(nèi)置編輯器 在Run Designer的Columns選中需要變更編輯方式的Column,在ColumnEdit 屬性的下拉菜單中選擇編輯數(shù)據(jù)使用的控件。 例1:Person表的CountryID字段的值來自Country表,使用下拉列表顯示CountryName編輯 修改CountryIDColumn.ColumnEdit值,選new->LookupEdit,默認命
11、名為repositoryItemLookUpEdit1。展開ColumnEdit屬性,將DisplayMember 設為CountryName,DropDownRows是下拉列表的行數(shù),ValueMember設為CountryID。 代碼中添加: /init data repositoryItemLookUpEdit1.DataSource = ds.TablesCountry; 例2:字段Age是整型,需要使用SpinEdit編輯 修改AgeColumn.ColumnEdit值,選new->SpinEdit。展開ColumnEdit屬性,修改MaxValue、MinValue設定最大、
12、最小值。運行時Age的取值只能在MaxValue至MinValue之間選值。s 7、 GridView調(diào)節(jié)行高顯示大文本 默認情況下gridview已單行方式顯示,過長的文本只能顯示開頭部分,鼠標停留在單元格上方有ToolTip顯示所有文本。在文本單元格的右邊兩個按鈕供切換顯示上下行。若需要在單元格變更行高顯示所有文本。使用 gridView1.OptionsView.RowAutoHeight = True; gridView1.LayoutChanged(); 也可以通過事件判斷文本內(nèi)容改變行高 代碼 private void gridView1_CalcRowHeight(object
13、sender,DevExpress.XtraGrid.Views.Grid.RowHeightEventArgs e) if(e.RowHandle >= 0) e.RowHeight = (int)gridView1.GetDataRow(e.RowHandle)"RowHeight" 8、 數(shù)據(jù)導出 XtraGrid支持Html、Xml、Txt、Xsl導出,對應的導出器是ExportHtmlProvider、ExportXmlProvider、ExportTxtProvider、ExportXslProvider 例:使用html格式導出數(shù)據(jù) 代碼 IExport
14、Provider provider = new ExprotHtmlProvider(filename); ExportTo(provider); private void ExportTo(IExportProvider provider) Cursor currentCursor = Cursor.Current; Cursor.Current = Cursors.WaitCursor; this.FindForm().Refresh(); BaseExportLink link = gridView1.CreateExportLink(provider); (link as GridVi
15、ewExportLink).ExpandAll = false; link.Progress += new DevExpress.XtraGrid.Export.ProgressEventHandler(Export_Progress);/進度條事件 link.ExportTo(true); provider.Dispose(); link.Progress -= new DevExpress.XtraGrid.Export.ProgressEventHandler(Export_Progress); Cursor.Current = currentCursor; 9、 焦點單元格顯示方式 G
16、rideView默認的焦點單元格顯示方式是整行選中,焦點單元格高亮。可以調(diào)整以下屬性進行修改 代碼 gridView1.FocusRectStyle :焦點繪畫方式 默認DrawFocusRectStyle.CellFocus(單元格)/ DrawFocusRectStyle.RowFocus(行)/ DrawFocusRectStyle.None(不繪畫) bool gridView1.OptionsSelection.EnableAppearanceFocusedCell :是否焦點顯示選中的單元格 bool gridView1.OptionsSelection.EnableAppeara
17、nceFocusedRow :是否焦點顯示選中的行 bool gridView1.OptionsSelection.InvertSelection :是否反顯示 bool gridView1.OptionsSelection.MultiSelect:是否使用多選 10、 顯示非數(shù)據(jù)源的數(shù)據(jù)devexpress.xtragrid.views.grid.gridview 格式化單元格分類: C# windows forms 2011-04-06 14:15 247人閱讀 評論(0) 收藏 舉報實現(xiàn)CustomColumnDisplayText事件(只能用于顯示)private void bande
18、dGridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e) if(e.Column.FiledName="fmapnumber") DataRow row = bandedGridView1.GetDataRow(e.RowHandle); e.DisplayText = string.Format("0","xxx"); 可以在GrideView上增加數(shù)據(jù)源沒有
19、的列,如合計、日期、序號或其他數(shù)據(jù)源等。 方法一:實現(xiàn)CustomColumnDisplayText事件(只能用于顯示) 以下例子在bandedGridColumn1上顯示 FirstName+LastName 代碼 bandedGridColumn1.OptionsColumn.AllowEdit = false; private void bandedGridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e) if(e.
20、Column.Equals(bandedGridColumn1) DataRow row = bandedGridView1.GetDataRow(e.RowHandle); e.DisplayText = string.Format("0 1", row"FirstName", row"LastName"); 方法二: 設定列的UnboundType,并實現(xiàn)CustomUnboundColumnData事件(可修改值) 以下例子演示DateTime/Int/String 綁定到數(shù)據(jù)列上顯示。當修改 GrideView上的值時,將修改
21、同步到原數(shù)組中。 代碼 bandedGridColumn4.UnboundType = DevExpress.Data.UnboundColumnType.DateTime; bandedGridColumn5.UnboundType = DevExpress.Data.UnboundColumnType.Integer; bandedGridColumn6.UnboundType = DevExpress.Data.UnboundColumnType.String; private void bandedGridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) if(array = null) return; if(e.ListSourceRowIndex >= array.Count) return; Record rec = arraye.ListSourceRowIndex as Record; if(rec = null) return; switch(e.Column.UnboundType) case UnboundColumnType.Date
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024園林苗木購銷合同范本
- 2024土地流轉(zhuǎn)規(guī)范合同書
- 2.1位置(拔尖作業(yè))2024-2025學年五年級上冊數(shù)學人教版(含解析)
- 質(zhì)量課件下載教學課件
- 3.3.2 海陸的變遷(教學設計)七年級地理上冊同步高效備課課件(人教版2024)
- Unit 1 How can we become good learners-解析版-單元測試卷九年級英語上冊單元速記·巧練(貴州專用人教版)
- 05 正確使用詞語(知識梳理+考點精講精練+實戰(zhàn)訓練)-【學考復習】2024年高中語文學業(yè)水平考試考點歸納與測試(新教材專用)(原卷版 )
- 部編版一年級下冊語文期末考試試題及答案
- 一氧化碳中毒課件
- 橋梁板相關項目投資計劃書
- 二氧化硫 教學設計 高一下學期化學人教版(2019)必修第二冊
- 臨建活動板房合同模板
- 2023-2024學年大同市八年級語文上學期期中考試卷附答案解析
- 第2章 實數(shù) 單元測試2024-2025學年北師大版八年級數(shù)學上冊
- 燃燒仿真軟件:OpenFOAM在發(fā)動機燃燒仿真中的應用
- 10的再認識教學設計公開課
- 2023-2024學年人教版八年級英語上冊期中測試試卷(含答案)
- 人工智能賦能教育評價改革的優(yōu)化策略研究
- 2024年6月高考地理真題完全解讀(浙江?。?/a>
- 中國高血壓防治指南(2024年修訂版)圖文解讀
- 造價鑒定合同范本
評論
0/150
提交評論