当前位置: 首页 > news >正文

c#开发_Dev的关于XtraGrid的使用(GridControl小结)_First

1,增加新行用InitNewRow事件,给新行某字段赋值。后结束编辑。

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,设为TopBottom可以在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通过实现IEditableObjectBeginEditCancelEdit方法,数据编辑后恢复特定数据。不使用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 DesignerColumns选中需要变更编辑方式的Column,在ColumnEdit 属性的下拉菜单中选择编辑数据使用的控件。

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


 

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

代码中添加:

//init data

repositoryItemLookUpEdit1.DataSource = ds.Tables[Country];


 

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

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

 

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


 

默认情况下gridview已单行方式显示,过长的文本只能显示开头部分,鼠标停留在单元格上方有ToolTip显示所有文本。在文本单元格的右边两个按钮供切换显示上下行。若需要在单元格变更行高显示所有文本。使用

gridView1.OptionsView.RowAutoHeight = True;

gridView1.LayoutChanged();

private void gridView1_CalcRowHeight(object sender,DevExpress.XtraGrid.Views.Grid.RowHeightEventArgs e) 
 {
 if(e.RowHandle >= 0)
 e.RowHeight = (int)gridView1.GetDataRow(e.RowHandle)["RowHeight"];
 }
 

8、 数据导出

XtraGrid支持HtmlXmlTxtXsl导出,对应的导出器是ExportHtmlProviderExportXmlProviderExportTxtProviderExportXslProvider

例:使用html格式导出数据

IExportProvider 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 GridViewExportLink).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、 焦点单元格显示方式

GrideView默认的焦点单元格显示方式是整行选中,焦点单元格高亮。可以调整以下属性进行修改

gridView1.FocusRectStyle :焦点绘画方式 默认DrawFocusRectStyle.CellFocus(单元格)/ DrawFocusRectStyle.RowFocus(行)/ DrawFocusRectStyle.None(不绘画)

bool gridView1.OptionsSelection.EnableAppearanceFocusedCell :是否焦点显示选中的单元格

bool gridView1.OptionsSelection.EnableAppearanceFocusedRow :是否焦点显示选中的行

bool gridView1.OptionsSelection.InvertSelection :是否反显示

bool gridView1.OptionsSelection.MultiSelect:是否使用多选

 

10
显示非数据源的数据

可以在GrideView上增加数据源没有的列,如合计、日期、序号或其他数据源等。

方法一:实现CustomColumnDisplayText事件(只能用于显示)

以下例子在bandedGridColumn1上显示 FirstName+LastName

 bandedGridColumn1.OptionsColumn.AllowEdit = false;
 private void bandedGridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
 {
      if(e.Column.Equals(bandedGridColumn1)) 
 {
          DataRow row = bandedGridView1.GetDataRow(e.RowHandle);
          e.DisplayText = string.Format("{0} {1}", row["FirstName"], row["LastName"]);
      } 
 }
 

 

方法二: 设定列的UnboundType,并实现CustomUnboundColumnData事件(可修改值)

以下例子演示DateTime/Int/String 绑定到数据列上显示。当修改 GrideView上的值时,将修改同步到原数组中。

 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 = array[e.ListSourceRowIndex] as Record;
               if(rec == null) return;
               switch(e.Column.UnboundType) {
                    case UnboundColumnType.DateTime:
                        if(e.IsGetData)
                             e.Value = rec.Date;
                        else rec.Date = (DateTime)e.Value;
                        break;
                    case UnboundColumnType.Integer:
                        if(e.IsGetData)
                             e.Value = rec.Count;
                        else rec.Count = (Int32)e.Value;
                        break;
                    case UnboundColumnType.String:
                        if(e.IsGetData)
                             e.Value = rec.Comment;
                        else rec.Comment = e.Value.ToString();
                        break;
               }
          }
 

提交当前行的修改

using DevExpress.XtraGrid;
 using DevExpress.XtraGrid.Views.Base;
 using System.Data.Common;
 //...
 public void UpdateDatasource(GridControl grid) {
     //Save the latest changes to the bound DataTable
     ColumnView view = (ColumnView)grid.FocusedView;
     view.CloseEditor();
     if(!view.UpdateCurrentRow()) return;
     
     //Update the database's Suppliers table to which oleDBDataAdapter1 is connected
     DoUpdate(oleDbDataAdapter1, dataSet11.Tables["Suppliers"]);
     //Update the database's Products table to which the oleDbDataAdapter2 is connected
     DoUpdate(oleDbDataAdapter2, dataSet11.Tables["Products"]);
 }
  
 public void DoUpdate(DbDataAdapter dataAdapter, System.Data.DataTable dataTable) {
     try {
         dataAdapter.Update(dataTable);
     } catch(Exception ex) {
         MessageBox.Show(ex.Message);
     }
 }
 

就写到这里吧,太长看得眼睛很累的
提供下关于GridControl的心得_Second链接http://www.cnblogs.com/stephen-vv/archive/2012/12/18/2822891.html

 

 

转载于:https://www.cnblogs.com/stephen-vv/archive/2012/12/18/2822800.html

相关文章:

  • 【JQuery】可直接编辑的表格
  • BE恢复单个邮箱
  • 医疗系统发展分析(转)
  • 在VMware Workstation 9上跑Win2012 HyperV
  • 对TCL/TK的一个小总结
  • httpclient完全支持ssl连接方式
  • 《高效能人士的7种心态》读后感+一年实施结果
  • 浅谈字节序(Endianness)
  • Exchange 2003队列中堆积大量垃圾邮件解决方法?
  • JavaScript面向对象编程深入分析(3)
  • 单例模式 c++实现
  • 用MDT 2012为企业部署windows 7(六)--Deploymentshare$常用文件夹介绍
  • 如何区分ripv1和ripv2?
  • Hadoop系列之九:Hadoop集群伪分布式模式的实现详解
  • 《C#线程参考手册》读书笔记(二):.NET中的线程
  • 2017前端实习生面试总结
  • flask接收请求并推入栈
  • iOS高仿微信项目、阴影圆角渐变色效果、卡片动画、波浪动画、路由框架等源码...
  • ReactNative开发常用的三方模块
  • React系列之 Redux 架构模式
  • SpiderData 2019年2月13日 DApp数据排行榜
  • SSH 免密登录
  • ucore操作系统实验笔记 - 重新理解中断
  • zookeeper系列(七)实战分布式命名服务
  • 初探 Vue 生命周期和钩子函数
  • 从tcpdump抓包看TCP/IP协议
  • 前端面试之闭包
  • 适配iPhoneX、iPhoneXs、iPhoneXs Max、iPhoneXr 屏幕尺寸及安全区域
  • 双管齐下,VMware的容器新战略
  • 推荐一款sublime text 3 支持JSX和es201x 代码格式化的插件
  • FaaS 的简单实践
  • ​LeetCode解法汇总2670. 找出不同元素数目差数组
  • #pragma once
  • #我与Java虚拟机的故事#连载04:一本让自己没面子的书
  • (173)FPGA约束:单周期时序分析或默认时序分析
  • (MATLAB)第五章-矩阵运算
  • (八)c52学习之旅-中断实验
  • (附源码)springboot 校园学生兼职系统 毕业设计 742122
  • (附源码)springboot助农电商系统 毕业设计 081919
  • (附源码)SSM环卫人员管理平台 计算机毕设36412
  • (接口自动化)Python3操作MySQL数据库
  • (未解决)macOS matplotlib 中文是方框
  • (转)项目管理杂谈-我所期望的新人
  • (转载)Linux网络编程入门
  • .NET 将多个程序集合并成单一程序集的 4+3 种方法
  • .NET 应用启用与禁用自动生成绑定重定向 (bindingRedirect),解决不同版本 dll 的依赖问题
  • .netcore 获取appsettings
  • .Net接口调试与案例
  • .NET开发不可不知、不可不用的辅助类(三)(报表导出---终结版)
  • .NET中的十进制浮点类型,徐汇区网站设计
  • .Net组件程序设计之线程、并发管理(一)
  • ??在JSP中,java和JavaScript如何交互?
  • @RequestMapping 的作用是什么?
  • [ 攻防演练演示篇 ] 利用通达OA 文件上传漏洞上传webshell获取主机权限
  • [Android]常见的数据传递方式