当前位置:首页 > Windows程序 > 正文

Dev的关于XtraGrid的使用(GridControl小结)

2021-05-24 Windows程序

private void grdView_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e) { DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView; view.SetRowCellValue(e.RowHandle, view.Columns["EnterID"], this.dS_MEnterStoreView.MEnterStore[0].ID); this.grdControl.EmbeddedNavigator.Buttons.EndEdit.DoClick(); this.grdView.UpdateCurrentRow(); }

2,如果进行行验证,就在换行时时行,用grdView_FocusedRowChanged事件

private void grdView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) { WsClient.WS_MEnterStore.DS_MEnterStoreView.MEnterDetailRow row = (WsClient.WS_MEnterStore.DS_MEnterStoreView.MEnterDetailRow)this.grdView.GetDataRow(e.FocusedRowHandle); if (row != null) { if ((this.OperState == Common.Enum.TOperState.UnConfirmNew) || (this.OperState == Common.Enum.TOperState.UnConfirmEdit)) { this.InitComboBoxValue(row, row.IsGoodIDNull()?0:row.GoodID, false); this.InitBatchComboBoxValue(row, row.IsGoodIDNull()?0:row.GoodID, false); } } }

3,如果需要改变行的某一列的同时改变其它的列用grdView_CellValueChanged事件

private void grdView_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e) { if (this.grdView.FocusedColumn == e.Column) { if (e.Column == this.colAmount) { } } }

4,如果需在离开行的时候需要验证,则用grdView_BeforeLeaveRow事件.

DevExpress XtraGrid的功能实在强大,刚使用的时候看到一大片属性设置,分不清东南西北,参照demo和使用中的一些经验,记录一下使用方法。现在数据库访问都使用ORM技术了,对于DataSouce绑定以下是以IList为说明对象。

控件基本定义   DevExpress.XtraGrid.GridControl gridControl1;

1、 数据绑定(IList)

DevExpress.XtraGrid.Views.Grid.GridView gridView1; IList<MyClass> list = new BindingList<MyClass>(); //初始list list.Add(A); list.Add(B); ……….. gridControl1.DataSource = list;

2、 在Grid上编辑数据

修改属性gridView1.OptionsView.NewItemRowPosition,设为Top或Bottom可以在Grid上添加数据。

(在demo中原文:a record object must be inherited from the IEditableObject class if you need the ability to cancel newly added records via the grid)

译:如果你需要通过gird取消新建的记录,你的记录对象必须实现IEditableObject

(注:在测试中,感觉不需要继承IEditableObject,在grid编辑后也能实现取消。demo通过实现IEditableObject的BeginEdit、CancelEdit方法,数据编辑后恢复特定数据。不使用grid直接修改数据,可以考虑这种恢复原数据的方法。)

3、 修改列(Column)格式

DevExpress.XtraGrid.Columns.GridColumn col = gridView1.Columns[0];

数据对齐方式 col.AppearanceCell.TextOptions.HAlignment, 默认值Default,可选值Default/Near/Center/Far。

说明:以下情况是基于从左到右的文字排列;若是从右到左,用法相反。

Default:数据默认的对齐方式

Near:左对齐

Center:居中对齐

Far:右对齐

列标题 col.Caption

对应绑定数据的属性 col.FieldName

排列顺序 col.VisibleIndex

格式化显示数据

Col.DisplayFormat.FormatType

Col.DisplayFormat.Format

Col.DisplayFormat.FormatString

区别:FormatType/FormatString 使用当前系统的语言区域设置,Format使用特定的

4、 使用Grid内置导航栏

gridControl1.UseEmbeddedNativgator=True

设定内置导航栏按钮其他属性 gridControl1.EmbeddedNavigator

5、 GridView内置方式编辑数据

禁止编辑数据 gridView1.OptionsBehavior.Editable = False,默认是True 可编辑。

Gridview内置数据编辑器显示方式 gridView1.OptionsBehavior.EditorShowMode,可选值Default/ MouseDown/MouseUp/ Click。

说明:

Default 多选Cell相当于Click,单选Cell相当于MouseDown

MouseDown 在单元格内按下鼠标键时打开内置编辑器

MouseUp 在单元格内释放鼠标键时打开内置编辑器

Click 在不是编辑状态,但获得焦点的单元格中点击时打开编辑器。点击非焦点单元格时,首先会切换焦点,再点击时才打开编辑器

6、 设定GrideView单元格的内置编辑器

在Run Designer的Columns选中需要变更编辑方式的Column,在ColumnEdit 属性的下拉菜单中选择编辑数据使用的控件。

例1:Person表的CountryID字段的值来自Country表,使用下拉列表显示CountryName编辑

修改CountryIDColumn.ColumnEdit值,选new->LookupEdit,默认命名为repositoryItemLookUpEdit1。展开ColumnEdit属性,将DisplayMember 设为CountryName,DropDownRows是下拉列表的行数,ValueMember设为CountryID。

代码中添加:

//init data

repositoryItemLookUpEdit1.DataSource = ds.Tables[Country];

例2:字段Age是整型,需要使用SpinEdit编辑

修改AgeColumn.ColumnEdit值,选new->SpinEdit。展开ColumnEdit属性,修改MaxValue、MinValue设定最大、最小值。运行时Age的取值只能在MaxValue至MinValue之间选值。

7、 GridView调节行高显示大文本

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/70320.html