GridControl 通用类2
gridView1.FocusedRowHandle; 获取当前选中行索引
//获取指定行 指定数据列的值
gridView.GetRowCellValue(Row, gridViewSap.Columns["ERP_QTY"]).ToString().Trim();
设置单元格的值
gridView.SetRowCellValue(Row, "ERP_QTY", "0");
public class UsrConGridView : GridControl { /* ============================================================================================================ 1、DataCol :须字段:FileNames 数据库字段,REM 字段中文描述,LONG 字段长度,type2数值类型,DecimalSize 小数位数; 2、直接调用方法ViewHeadStyle(数据表,菜单路径); ----------------------------------------------- ============================================================================================================*/ /// <summary> /// 加载GridView抬头 和 样式 /// </summary> /// <param name="dt">列的字段表</param> /// <param name="navpath">所属导航菜单路径</param> public void ViewHeadStyle(DataTable dt, string navpath) { this.DataCol = dt;//配置表:标题列头的字段属性 this.NavPath = navpath; //所属窗体对应的导航菜单路径 ServerPath = System.Windows.Forms.Application.StartupPath + "\\layout\\"; if (!Directory.Exists(ServerPath)) { Directory.CreateDirectory(ServerPath); } XmlFile = ServerPath + NavPath.Replace(".", "") + gridView.Name + ".xml"; ViewHead(dt, gridView);//加载 标题列的详细信息,以及配置字段格式 ViewStyle(gridView);//设置窗格样式,以及触发事件 DataTable dtitm = new DataTable(); string FileNames = "", FILETYPE = ""; try { for (int i = 0; i < dt.Rows.Count; i++) { Type t = Type.GetType("System.String"); FileNames = dt.Rows[i]["FileNames"].ToString(); FILETYPE = dt.Rows[i]["FILETYPE"].ToString(); //数据类型 string Ctype = new HelpCs().helpBase.SqlToCSharpType(FILETYPE); if (Ctype != "") { t = Type.GetType(Ctype); } dtitm.Columns.Add(dt.Rows[i]["FileNames"].ToString(), t); } this.DataSource = dtitm; } catch (Exception ex) { return; } } private GridView gridview; /// <summary> /// GridView /// </summary> public GridView gridView { get { try { gridview = this.MainView as GridView; } catch (Exception) { gridview = new GridView(); } if (gridview == null) { gridview = new GridView(); gridview.GridControl = this; this.MainView = gridview; } return this.MainView as GridView; } set { gridview = value; } } /*================================================================*/ //默认值,可以不配置。 private int indicatorWidth = 80; /// <summary> /// 表格负一(-1)列的宽度 /// </summary> [Category("自定义"), Description("Z表格负一(-1)列的宽度"), Browsable(true)] public int IndicatorWidth { get { return indicatorWidth; } set { indicatorWidth = value; } } [Category("自定义"), Description("Z窗格样式保存路径"), Browsable(true)] public string ServerPath { get;set; } private Color oddRowBackColor = Color.PaleGoldenrod; [Category("自定义"), Description("Z控件奇数行背景色"), Browsable(true)] public Color OddRowBackColor { get { return oddRowBackColor; } set { oddRowBackColor = value; } } private Color evenRowBackColor = Color.WhiteSmoke; [Category("自定义"), Description("Z控件偶数行背景色"), Browsable(true)] public Color EvenRowBackColor { get { return evenRowBackColor; } set { evenRowBackColor = value; } } private Color focusedRowForeColor = Color.Red;//默认红色 [Category("自定义"), Description("Z选中行字体颜色"), Browsable(true)] public Color FocusedRowForeColor { get { return focusedRowForeColor; } set { focusedRowForeColor = value; } } private Color focusedRowBackColor ; [Category("自定义"), Description("Z选中行背景颜色"), Browsable(true)] public Color FocusedRowBackColor { get { return focusedRowBackColor; } set { focusedRowBackColor = value; } } /*================================================================*/ //必须配置,如果不赋值,默认父类路径 private string navPath = ""; [Category("自定义"), Description("Z控件所在窗体的窗体菜单路径"), Browsable(true)] public string NavPath { get { if (navPath == "") { StackTrace ss = new StackTrace(true); MethodBase mb = ss.GetFrame(1).GetMethod(); navPath = mb.DeclaringType.FullName;//取得父类全名,包括程序集和路径、类名 } return navPath; } set { navPath = value; } } //private string xmlfile = ""; [Category("自定义"), Description("Z窗格样式保存路径的文件完整路径"), Browsable(true)] public string XmlFile { get;set; } private DataTable dataCol; [Category("自定义"), Description("Z表格加载列"), Browsable(true)] public DataTable DataCol { get { return dataCol; } set { dataCol = value; } } /// <summary> /// 编辑列头 /// </summary> /// <param name="dt">列头 以及 列属性 表</param> /// <param name="gv">GridView对象</param> public void ViewHead(DataTable dt, GridView gv) { string FileNames = "", REM = "", LONG = "", type2 = "", DecimalSize = ""; try { for (int i = 0; i < dt.Rows.Count; i++) { FileNames = dt.Rows[i]["FileNames"].ToString(); REM = dt.Rows[i]["REM"].ToString(); LONG = dt.Rows[i]["LONG"].ToString(); type2 = dt.Rows[i]["type2"].ToString();//数值类型:数值、文本、时间、 DecimalSize = dt.Rows[i]["DecimalSize"].ToString();//数值类型,小数位数长度。 DevExpress.XtraGrid.Columns.GridColumn Col1 = new DevExpress.XtraGrid.Columns.GridColumn(); try { Col1.Width = Convert.ToInt32(LONG); } catch (Exception ex) { Col1.Width = 100; } Col1.Tag = FileNames; Col1.Name = "Col" + FileNames; Col1.FieldName = FileNames; Col1.Caption = REM; if (type2 == "时间") { Col1.DisplayFormat.FormatType = FormatType.DateTime; Col1.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss"; } if (type2 == "数值") { Col1.DisplayFormat.FormatString = "N"+ DecimalSize;//设置小数位数 Col1.DisplayFormat.FormatType = FormatType.Numeric; } Col1.VisibleIndex = i; gv.Columns.Add(Col1); } } catch (Exception) { } } /// <summary> /// GridContrl--GridView 的设置 /// </summary> /// <param name="gridView1">gridView1</param> /// <param name="IndicatorWidth">-1 列的宽度</param> /// <param name="dt"> 绑定的数据源</param> /// <param name="ColCaption">列标题</param> public void ViewStyle(GridView gridView1) { //是否允许多选,是True gridView1.OptionsSelection.MultiSelect = true; //多选模式设置 gridView1.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CellSelect; //设置列索引为-1的列宽 gridView1.IndicatorWidth = IndicatorWidth; //表头行高 设置为28; gridView1.ColumnPanelRowHeight = 28; //内容行高 设置为22; gridView1.RowHeight = 22; //行高22 //分组框高度 gridView1.GroupRowHeight = 35; //列标题垂直居中 : valign是上下的位置,align是左右的位置 ; gridView1.Appearance.HeaderPanel.Options.UseTextOptions = false; gridView1.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; gridView1.Appearance.HeaderPanel.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center; //行内容对齐方式:垂直居中,文字左对齐 gridView1.Appearance.Row.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;//左对齐 gridView1.Appearance.Row.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center; //HAlignment(水平位置):Far(数字右对齐)/ Near(文字、日期左对齐) //=================================================================== /*是否有权限,或开通功能,若没有开通,此功能就用不了*/ /*------------------------------------------------*/ //是否允许开通 列头漏斗筛选按钮; false 不允许用户进行过滤; 列头漏斗状的筛选按钮,点击后会弹出勾选框,跟Excel列头筛选功能类似; gridView1.OptionsCustomization.AllowFilter = true; //是否允许开通 按列分组功能:可以:true;不允许:false;如果设置为false,用户不能对每列进行分组查看报表; gridView1.OptionsCustomization.AllowGroup = true; //允许用户拖动列; 如果设置为false,不允许拖动列头; gridView1.OptionsCustomization.AllowColumnMoving = true; //不允许进行列排序; 如果设置为false,有上下三角形按钮,但是不能进行排序; gridView1.OptionsCustomization.AllowSort = true; //=================================================================== /*是否显示某模块,而非禁用或启用功能*/ /*------------------------------------------------*/ //显示或隐藏页脚信息框;显示:true默认值;不显示:false;框内可以设置每列的合计、最大、最小、平均值等等; gridView1.OptionsView.ShowFooter = true; //显示或隐藏分组框;这里放置在最后配置才有效果 gridView1.OptionsView.ShowGroupPanel = false; //显示或隐藏列头过滤行;false不允许显示 过滤行。模糊查询,设置他的属性为true时,会在表上面形成一个空行 gridView1.OptionsView.ShowAutoFilterRow = true; //是否显示右键菜单:false:不显示右键菜单 gridView1.OptionsMenu.EnableColumnMenu = true; //自动过滤时不显示过滤记录 gridView1.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.ShowAlways; //自动列宽 gridView1.BestFitColumns(); gridView1.HorzScrollVisibility = DevExpress.XtraGrid.Views.Base.ScrollVisibility.Always; //自动列宽,会出现横向滚动条 gridView1.OptionsView.ColumnAutoWidth = false; gridView1.Appearance.OddRow.BackColor = OddRowBackColor; // 设置奇数行颜色 // 默认也是白色 可以省略 gridView1.OptionsView.EnableAppearanceOddRow = true; // 使能 // 和和上面绑定 同时使用有效 gridView1.Appearance.EvenRow.BackColor = EvenRowBackColor; // 设置偶数行颜色 gridView1.OptionsView.EnableAppearanceEvenRow = true; // 使能 // 和和上面绑定 同时使用有效 //添加 行索引事件 gridView1.CustomDrawRowIndicator += gridView_CustomDrawRowIndicator; //布局变化时产生事件 gridView1.Layout += gridView_Layout; LoadLayout(gridView1);//加载窗格 } #region ================================公用事件====================================== /// <summary> /// 布局变化时产生事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void gridView_Layout(object sender, EventArgs e) { GridView gridView1 = (GridView)sender; SaveLayout(gridView1);//布局变化时 自动保存窗格布局 } /// <summary> /// 保存风格 /// </summary> /// <param name="moduleid"></param> /// <param name="saveType"></param> public void SaveLayout(GridView gridView1) { if (File.Exists(XmlFile)) { File.Delete(XmlFile); } gridView1.SaveLayoutToXml(XmlFile); } /// <summary> /// 加载风格 /// </summary> /// <param name="moduleid"></param> /// <param name="saveType"></param> public void LoadLayout(GridView gridView1) { if (!File.Exists(XmlFile)) { return; } gridView1.RestoreLayoutFromXml(XmlFile); } /// <summary> /// 设置选中行字体颜色 /// </summary> public void gridView_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) { GridView gridView1 = (GridView)sender; gridView1.Appearance.FocusedRow.ForeColor = FocusedRowForeColor;//字体颜色 // if (FocusedRowBackColor != null) { gridView1.Appearance.FocusedRow.BackColor = FocusedRowBackColor;//行背景颜色 } //gridView1.Appearance.FocusedRow.BackColor = Color.Linen;//行背景颜色 } /// <summary> /// 添加行号 和 筛选行筛选是不用输入百分号 % /// </summary> public static void gridView_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e) { GridView gridView1 = (GridView)sender; //设置行索引:列索引等于-1处设置行索引。 if (e.Info.IsRowIndicator && e.RowHandle >= 0) { e.Info.DisplayText = (e.RowHandle + 1).ToString(); } //设置筛选行不用输入百分号直接筛选 foreach (DevExpress.XtraGrid.Columns.GridColumn item in gridView1.Columns) { item.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains; //筛选条件设置为包含 } } #endregion ================================公用事件====================================== }