众所周知,WINForm 的 DataGrid 组件的功能强大而且灵活,作为一个数据库程序离开它可不行,但是dataGrid在设计期间采用“套用式样”的方式设计表格的外观并不够灵活,那有没有办法统一管理表格风格呢?答案是有。
作为数据表格的应用最重要的应该不是风格的颜色搭配,而是字段属性的格式化,要格式化每一列的数据显示值,比如说:dataGrid的记录集如果有个“日期时间”字段的话,就会发现,默认的话其实它只显示了日期部分,而时间被Format掉了,这样的话不能满足应用的需求。很多新手朋友也发过帖子询问如何显示出时间,本人也是属于新手行列,查阅了MSDN后编写了一个管理dataGrid 格式列和风格的类,它可以实现管理‘列宽、字体、标题名、格式类型、是否隐藏、行选、行只读、列只读’的基本功能。
先大致讲一下格式化列的原理。列的格式由DataGridTextBoxColumn的Format属性进行控制,Format 属性可以支持日期、货币、数值、文本等格式字符,具体信息可以参考MSDN:http://msdn.microsoft.com/library/CHS/cpref/html/frlrfSystemWindowsFormsDataGridTextBoxColumnClassFormatTopic.asp?frame=true。
这里演示格式化“日期时间”,代码:
DataGridTextBoxColumn gridColumn = DataGridTextBoxColumn();
gridColumn.Format=System.String.Format("yyyy-MM-dd hh:mm:ss",gridColumn.TextBox);
现在我把类的源码贴上面,我不保证代码的一些做法是否合理与正确,仅供参考:
using System; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Drawing; namespace Grid.Service { /**//// <summary> /// 格式化字符串 /// </summary> public enum FormatIndex{ Text = 0x0, //文本 Boolean, //是/否(钩选框) Money, //货币(¥#0.00) Numeric, //数值格式(#0.00) RealNumber, //实数(0.000000) LongDateTime, //长日期时间(yyyy年mm月dd日 hh:mm:ss) DateTime, //日期时间(yyyy-mm-dd hh:mm:ss) LongDate, //长日期(yyyy-mm-dd) ShortDate, //短日期(yy-mm-dd) Time //时间(hh:mm:ss) }; /**//// <summary> /// StyleManager 的摘要说明。 /// DataGrid 的属性管理类 /// </summary> public class StyleManager { public static int lastSelectIndex = -1; private DataGrid _dataGrid =null; private DataTable _dataTable =null; private bool _allowSelectedRow =false; private int[] ReadonlyRows = null; private bool isReadonlyRow = false; private bool _dataGridReadonly = false; public StyleManager(DataGrid dataGrid){ _dataGrid = dataGrid; } public StyleManager(){;} [Category("Action")] [Description("可操作的数据表格")] public DataGrid @DataGrid{ get{return _dataGrid;} set{ _dataGrid=value; this._dataGrid.CurrentCellChanged -= new System.EventHandler(this.dataGrid_CurrentCellChanged); if(this._allowSelectedRow){ if(_dataGrid!=null){ //修改事件 this._dataGrid.CurrentCellChanged += new System.EventHandler(this.dataGrid_CurrentCellChanged); } } } } [Category("Data")] [Description("要与数据表格绑定的数据源")] public DataTable DataSource{ get{return _dataTable;} set{_dataTable =(value as DataTable);} } [Category("Appearance")] [Description("获取当前的表格风格")] public DataGridTableStyle CurrentTableStyle{ get{return GetGridTableStyle();} } /**//// <summary> /// 允许行选择模式 /// </summary> [Category("Behavior")] [Description("允许行选择模式")] public bool AllowSelectedRow{ get{return _allowSelectedRow;} set{ _allowSelectedRow =value; this._dataGrid.CurrentCellChanged -= new System.EventHandler(this.dataGrid_CurrentCellChanged); if(value){ if(_dataGrid!=null){ //修改事件 this._dataGrid.CurrentCellChanged += new System.EventHandler(this.dataGrid_CurrentCellChanged); } } } } [Category("Behavior")] [Description("表格只读,也可隐藏底部的新增行")] public bool ReadOnly{ get{return _dataGridReadonly;} set{_dataGridReadonly = value;} } /**//// <summary> /// 行选择事件 /// </summary> /// <param name="sender">对象</param> /// <param name="e">事件参数</param> private void dataGrid_CurrentCellChanged(object sender, System.EventArgs e) { if(this.DataSource!=null){ try{ this._dataGrid.Select(this._dataGrid.CurrentRowIndex); if(StyleManager.lastSelectIndex!=-1){ this._dataGrid.UnSelect(StyleManager.lastSelectIndex); } StyleManager.lastSelectIndex = this._dataGrid.CurrentRowIndex; }catch{;} } //锁定表格行的算法 if(isReadonlyRow&&this.ReadonlyRows!=null){ this._dataGrid.ReadOnly = false; if(this.ReadonlyRows.Length>0) for(int i=0;i<ReadonlyRows.Length;i++){ if(this._dataGrid.CurrentRowIndex==ReadonlyRows[i]){ this._dataGrid.ReadOnly = true;break;
} } } if(_dataGridReadonly){ this._dataGrid.ReadOnly = true; return; } } /**//// <summary> /// 设置列头风格 /// </summary> /// <param name="HeaderFont">字体</param> /// <param name="HeaderFontColor">文字颜色</param> /// <returns>是否成功</returns> public bool SetHeader(Font HeaderFont,Color HeaderFontColor){ try{ DataGridTableStyle currGTS = GetGridTableStyle(); if(currGTS==null){ throw new Exception("初始化表风格出错!"); } currGTS.HeaderFont = HeaderFont; currGTS.HeaderForeColor = HeaderFontColor; return true; }catch{ return false; } } /**//// <summary> /// 设置列头风格,默认黑色字体颜色 /// </summary> /// <param name="HeaderFont">字体</param> /// <returns>是否成功</returns> public bool SetHeader(Font HeaderFont){ return SetHeader(HeaderFont,System.Drawing.Color.Black); } /**//// <summary> /// 设置表格风格 /// </summary> /// <param name="CaptionText">标题文本</param> /// <param name="CaptionVisible">标题可视</param> /// <param name="RowHeaderVisible">行头可视</param> /// <returns>是否成功</returns> public bool SetGrid(string CaptionText,bool CaptionVisible,bool RowHeaderVisible){ try{ DataGridTableStyle currGTS = GetGridTableStyle(); if(currGTS==null){ throw new Exception("初始化表风格出错!"); } if(this._dataGrid==null){ throw new Exception("无可操作的表格对象!"); } this._dataGrid.CaptionText = CaptionText; this._dataGrid.CaptionVisible = CaptionVisible; currGTS.RowHeadersVisible = RowHeaderVisible; return true; }catch{ return false; } } /**//// <summary> /// 根据数据类型获取列类型 /// </summary> /// <param name="tp">数据类型</param> /// <returns></returns> private int getDataGridExColumn(Type tp){ if(tp==System.Type.GetType("System.Boolean")){ return 2; }else if(tp==System.Type.GetType("System.DateTime")){ return 6; }else if(tp==System.Type.GetType("System.Decimal")){ return 1; }else if(tp==System.Type.GetType("System.Single")||tp==System.Type.GetType("System.Double")) { return 3; }else{ return 0; } } /**//// <summary> /// 获取默认默认风格 /// </summary> /// <returns>表风格</returns> private DataGridTableStyle GetGridTableStyle(){ try{ if (_dataGrid.TableStyles.Count<=0&&_dataTable==null) throw new ArgumentNullException("数据源为空!"); if((_dataGrid.DataSource as DataTable)!=_dataTable){ _dataGrid.DataSource = _dataTable; } if(_dataGrid.TableStyles.Count<=0){ DataGridTableStyle style = new DataGridTableStyle(); style.MappingName = _dataTable.TableName; foreach(DataColumn column in _dataTable.Columns) { if(column.DataType==System.Type.GetType("System.Boolean")){ DataGridBoolColumn gridColumn = new DataGridBoolColumn(); gridColumn.TrueValue = true; gridColumn.FalseValue = false; gridColumn.MappingName = column.ColumnName; gridColumn.HeaderText = column.ColumnName; gridColumn.Width = _dataGrid.PreferredColumnWidth; style.GridColumnStyles.Add(gridColumn); }else{ DataGridTextBoxColumn gridColumn = new DataGridTextBoxColumn(); gridColumn.MappingName = column.ColumnName; gridColumn.HeaderText = column.ColumnName; gridColumn.Width = _dataGrid.PreferredColumnWidth; gridColumn.NullText = ""; style.GridColumnStyles.Add(gridColumn); } } _dataGrid.TableStyles.Clear(); _dataGrid.TableStyles.Add(style); } return _dataGrid.TableStyles[0]; }catch{ return null; } } private bool isBoolColumn(string ColumnName){ return (_dataTable.Columns[ColumnName].DataType==System.Type.GetType("System.Boolean")); } private int getColumnIndex(DataGridTableStyle DGTS , string columName){ for(int i=0;i<DGTS.GridColumnStyles.Count;i++){ if(DGTS.GridColumnStyles[i].HeaderText.ToString().Trim()==columName.ToString().Trim()){ return i; } } return -1; } /**//// <summary> /// 设置只读的行 /// </summary> /// <param name="rowIndexs">行索引数组</param> /// <param name="Readonly">是否只读</param> /// <returns>是否成功</returns> public bool SetReadOnlyRows(int[] rowIndexs,bool Readonly){ try{ this.isReadonlyRow = Readonly; this.ReadonlyRows = rowIndexs; return true; }catch{ return false; } } /**//// <summary> /// 设置列只读属性 /// </summary> /// <param name="columnIndexs">列索引数组</param> /// <param name="Readonly">只读属性</param> /// <returns>是否处理成功</returns> public bool SetReadOnlyColumns(int[] columnIndexs,bool Readonly){ //获取表风格 try{ DataGridTableStyle currGTS = GetGridTableStyle(); if(currGTS==null){ throw new Exception("初始化表风格出错!"); } if(columnIndexs==null||columnIndexs.Length<=0){ throw new ArgumentOutOfRangeException("只读索引超出范围!"); } DataGridColumnStyle GCS; for(int i=0;i<columnIndexs.Length;i++){ if(columnIndexs[i]<0&&columnIndexs[i]>currGTS.GridColumnStyles.Count-1){ throw new ArgumentOutOfRangeException("索引超出范围!"); } GCS = currGTS.GridColumnStyles[columnIndexs[i]]; if(GCS is DataGridBoolColumn){ (GCS as DataGridBoolColumn).ReadOnly = Readonly; }else if(GCS is DataGridTextBoxColumn){ (GCS as DataGridTextBoxColumn).ReadOnly = Readonly; } } return true; }catch{return false;} } /**//// <summary> /// 设置列只读属性 /// </summary> /// <param name="columnNames">列名称数组</param> /// <param name="Readonly">只读属性</param> /// <returns>是否处理成功</returns> public bool SetReadOnlyColumns(string[] columnNames,bool Readonly){ //获取表风格 try{ DataGridTableStyle currGTS = GetGridTableStyle(); if(currGTS==null){ throw new Exception("初始化表风格出错!"); } if(columnNames==null||columnNames.Length<=0){ throw new ArgumentOutOfRangeException("只读索引超出范围!"); } DataGridColumnStyle GCS; int columnIndex =-1; for(int i=0;i<columnNames.Length;i++){ columnIndex = getColumnIndex(currGTS,columnNames[i]); if(columnIndex==-1){ throw new Exception("列名称为空或者无效!"); } GCS = currGTS.GridColumnStyles[columnNames[i]]; if(GCS is DataGridBoolColumn){ (GCS as DataGridBoolColumn).ReadOnly = Readonly; }else if(GCS is DataGridTextBoxColumn){ (GCS as DataGridTextBoxColumn).ReadOnly = Readonly; } } return true; }catch{return false;} } /**//// <summary> /// 设置列属性 /// </summary> /// <param name="columnIndex">列索引值</param> /// <param name="Width">宽度</param> /// <param name="Title">标题文本</param> /// <param name="Format">列格式</param> /// <param name="Align">靠齐方式</param> /// <param name="Visible">是否可见</param> /// <returns>完成状态</returns> public bool SetColumn(int columnIndex,int Width,string Title,FormatIndex Format,HorizontalAlignment Align,bool Visible){ try{ //获取表风格 DataGridTableStyle currGTS = GetGridTableStyle(); string MappingName= null; if(currGTS==null){ throw new Exception("初始化表风格出错!"); } if(columnIndex<0&&columnIndex>currGTS.GridColumnStyles.Count-1){ throw new ArgumentOutOfRangeException("索引超出范围!"); } //处理参数 Width = (Math.Abs(Width)); Title = (Title == null ? "" : Title); Format = ((object)Format == null ? FormatIndex.Text : Format); Align = ((object)Align == null ? HorizontalAlignment.Left : Align); MappingName = currGTS.GridColumnStyles[columnIndex].MappingName; DataGridColumnStyle GCS = currGTS.GridColumnStyles[columnIndex]; if(GCS is DataGridTextBoxColumn){ DataGridTextBoxColumn gridColumn = (GCS as DataGridTextBoxColumn); switch((int)Format){ case 2: gridColumn.Format = "C";break;
case 3: gridColumn.Format = "N";break;
case 5: gridColumn.Format=System.String.Format("yyyy年MM月dd日 hh:mm:ss",gridColumn.TextBox);break;
case 6: gridColumn.Format=System.String.Format("yyyy-MM-dd hh:mm:ss",gridColumn.TextBox);break;
case 7: gridColumn.Format=System.String.Format("yyyy-MM-dd",gridColumn.TextBox);break;
case 8: gridColumn.Format=System.String.Format("yy-MM-dd",gridColumn.TextBox);break;
case 9: gridColumn.Format= System.String.Format("hh:mm:ss",gridColumn.TextBox);break;
default: gridColumn.Format = "";break;
} gridColumn.Width = Width; gridColumn.MappingName = MappingName; gridColumn.HeaderText = (Title.ToString()==""||Title==null ? MappingName : Title.ToString()); gridColumn.Alignment = Align; gridColumn.NullText = ""; } //隐藏字段 if(!Visible){ GCS.Width =0; GCS.HeaderText =""; } return true; }catch{ return false; } } /**//// <summary> /// 设置列属性,默认可见 /// </summary> /// <param name="columnIndex">列索引值</param> /// <param name="Width">宽度</param> /// <param name="Title">标题文本</param> /// <param name="Format">列格式</param> /// <param name="Align">靠齐方式</param> /// <returns>完成状态</returns> public bool SetColumn(int columnIndex,int Width,string Title,FormatIndex Format,HorizontalAlignment Align){ return SetColumn(columnIndex,Width,Title,Format,Align,true); } /**//// <summary> /// 设置列属性,默认可见,默认靠齐居左 /// </summary> /// <param name="columnIndex">列索引值</param> /// <param name="Width">宽度</param> /// <param name="Title">标题文本</param> /// <param name="Format">列格式</param> /// <returns>完成状态</returns> public bool SetColumn(int columnIndex,int Width,string Title,FormatIndex Format){ return SetColumn(columnIndex,Width,Title,Format,HorizontalAlignment.Left,true); } /**//// <summary> /// 设置列属性,默认可见,默认靠齐居左,默认文本格式 /// </summary> /// <param name="columnIndex">列索引值</param> /// <param name="Width">宽度</param> /// <param name="Title">标题文本</param> /// <returns>完成状态</returns> public bool SetColumn(int columnIndex,int Width,string Title){ return SetColumn(columnIndex,Width,Title,FormatIndex.Text,HorizontalAlignment.Left,true); } /**//// <summary> /// 设置列属性,默认可见,默认靠齐居左,默认文本格式,默认标题为影射名 /// </summary> /// <param name="columnIndex">列索引值</param> /// <param name="Width">宽度</param> /// <returns>完成状态</returns> public bool SetColumn(int columnIndex,int Width){ return SetColumn(columnIndex,Width,"",FormatIndex.Text,HorizontalAlignment.Left,true); } /**//// <summary> /// 设置列属性 /// </summary> /// <param name="columnName">列名称</param> /// <param name="Width">宽度</param> /// <param name="Title">标题文本</param> /// <param name="Format">列格式</param> /// <param name="Align">靠齐方式</param> /// <param name="Visible">是否可见</param> /// <returns>完成状态</returns> public bool SetColumn(string columnName,int Width,string Title,FormatIndex Format,HorizontalAlignment Align,bool Visible){ DataGridTableStyle currGTS = GetGridTableStyle(); if(currGTS==null){ throw new Exception("初始化表风格出错!"); } int columnIndex = getColumnIndex(currGTS,columnName); if(columnIndex==-1){ throw new Exception("列名称为空或者无效!"); } return SetColumn(columnIndex,Width,Title,Format,Align,Visible); } /**//// <summary> /// 设置列属性,默认可见 /// </summary> /// <param name="columnName">列名称</param> /// <param name="Width">宽度</param> /// <param name="Title">标题文本</param> /// <param name="Format">列格式</param> /// <param name="Align">靠齐方式</param> /// <returns>完成状态</returns> public bool SetColumn(string columnName,int Width,string Title,FormatIndex Format,HorizontalAlignment Align){ DataGridTableStyle currGTS = GetGridTableStyle(); if(currGTS==null){ throw new Exception("初始化表风格出错!"); } int columnIndex = getColumnIndex(currGTS,columnName); if(columnIndex==-1){ throw new Exception("列名称为空或者无效!"); } return SetColumn(columnIndex,Width,Title,Format,Align); } /**//// <summary> /// 设置列属性,默认可见,默认靠齐居左 /// </summary> /// <param name="columnName">列名称</param> /// <param name="Width">宽度</param> /// <param name="Title">标题文本</param> /// <param name="Format">列格式</param> /// <returns>完成状态</returns> public bool SetColumn(string columnName,int Width,string Title,FormatIndex Format){ DataGridTableStyle currGTS = GetGridTableStyle(); if(currGTS==null){ throw new Exception("初始化表风格出错!"); } int columnIndex = getColumnIndex(currGTS,columnName); if(columnIndex==-1){ throw new Exception("列名称为空或者无效!"); } return SetColumn(columnIndex,Width,Title,Format); } /**//// <summary> /// 设置列属性,默认可见,默认靠齐居左,默认文本格式 /// </summary> /// <param name="columnName">列名称</param> /// <param name="Width">宽度</param> /// <param name="Title">标题文本</param> /// <returns>完成状态</returns> public bool SetColumn(string columnName,int Width,string Title){ DataGridTableStyle currGTS = GetGridTableStyle(); if(currGTS==null){ throw new Exception("初始化表风格出错!"); } int columnIndex = getColumnIndex(currGTS,columnName); if(columnIndex==-1){ throw new Exception("列名称为空或者无效!"); } return SetColumn(columnIndex,Width,Title); } /**//// <summary> /// 设置列属性,默认可见,默认靠齐居左,默认文本格式,默认标题为影射名 /// </summary> /// <param name="columnName">列名称</param> /// <param name="Width">宽度</param> /// <returns>完成状态</returns> public bool SetColumn(string columnName,int Width){ DataGridTableStyle currGTS = GetGridTableStyle(); if(currGTS==null){ throw new Exception("初始化表风格出错!"); } int columnIndex = getColumnIndex(currGTS,columnName); if(columnIndex==-1){ throw new Exception("列名称为空或者无效!"); } return SetColumn(columnIndex,Width); } } }下面的代码是演示如何使用这个类:
OleDbConnection conn = new OleDbConnection( "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Data Source=\"E:\\sf\\DataBasew.mdb\";Mode=Share Deny None;Jet OLEDB:Engine Type=5;Provider=\"Microsoft.Jet.OLEDB.4.0\";Jet OLEDB:System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1" ); OleDbDataAdapter da = new OleDbDataAdapter("SELECT A.设备编号, b.设备名称, b.所属设备组, A.检测时间, A.气体压力, A.换算压力, A.气体湿度, A.气体温度, A.诊断代码 FR" + "OM (六氟化硫数据 A LEFT OUTER JOIN 测试设备 b ON A.设备编号 = b.编号) ORDER BY b.所属设备组, A.检测时间", conn ); DataSet ds = new DataSet(); try { da.Fill( ds, "六氟化硫数据" ); } catch( Exception ex ) { MessageBox.Show( ex.Message ); } //这里不需要制定,因为要由风格管理类接管,并且避免dataGrid自动初始化表风格影响速度 //dataGrid.DataSource = ds.Tables[0]; Grid.Service.StyleManager sm = new Grid.Service.StyleManager(); sm.DataGrid = dataGrid; //目标DataGrid sm.DataSource = ds.Tables[0]; //目标DataTable sm.AllowSelectedRow = true; //允许行选择 //设置第1列宽200,名称为“设备编号!",数据类型为数值型,局中,不隐藏 sm.SetColumn(0,200,"设备编号 !",Grid.Service.FormatIndex.Numeric,HorizontalAlignment.Center,true); //设置第2列宽100,不制定名称“用表字段真实名称”,文本类型 sm.SetColumn(1,100,(string)null,Grid.Service.FormatIndex.Text,HorizontalAlignment.Center,true); //设置第4列为日期时间类型 sm.SetColumn(3,200,(string)null,Grid.Service.FormatIndex.DateTime); sm.SetReadOnlyColumns(new int[]{0,2,5},true); //只读列0,2,5 sm.SetReadOnlyRows(new int[]{0,4,6,8},true);//只读行0,4,6,8 sm.SetHeader(new Font("黑体",9f),Color.Red); //设置列头字体,黑体9号字,红色 sm.SetGrid("测试表格",true,false); //显示表格标题,不显示行标题 conn.Close();原地址:http://www.cnblogs.com/chinasf/archive/2005/04/18/139506.html