DataGrid 风格管理类测试版源码

    众所周知,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);
类的基本流程:
I.jpeg
现在我把类的源码贴上面,我不保证代码的一些做法是否合理与正确,仅供参考:
None.gif using System;
None.gif using System.ComponentModel;
None.gif using System.Windows.Forms;
None.gif using System.Data;
None.gif using System.Drawing;
None.gif
ExpandedBlockStart.gif ContractedBlock.gif namespace Grid.Service dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 格式化字符串
ExpandedSubBlockEnd.gif
/// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic enum FormatIndexdot.gif{
InBlock.gif Text = 0x0, //文本
InBlock.gif
Boolean, //是/否(钩选框)
InBlock.gif
Money, //货币(¥#0.00)
InBlock.gif
Numeric, //数值格式(#0.00)
InBlock.gif
RealNumber, //实数(0.000000)
InBlock.gif
LongDateTime, //长日期时间(yyyy年mm月dd日 hh:mm:ss)
InBlock.gif
DateTime, //日期时间(yyyy-mm-dd hh:mm:ss)
InBlock.gif
LongDate, //长日期(yyyy-mm-dd)
InBlock.gif
ShortDate, //短日期(yy-mm-dd)
InBlock.gif
Time //时间(hh:mm:ss)
ExpandedSubBlockEnd.gif
};
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// StyleManager 的摘要说明。
InBlock.gif
/// DataGrid 的属性管理类
ExpandedSubBlockEnd.gif
/// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic class StyleManager dot.gif{
InBlock.gif
InBlock.gifpublic static int lastSelectIndex = -1;
InBlock.gifprivate DataGrid _dataGrid =null;
InBlock.gifprivate DataTable _dataTable =null;
InBlock.gifprivate bool _allowSelectedRow =false;
InBlock.gifprivate int[] ReadonlyRows = null;
InBlock.gifprivate bool isReadonlyRow = false;
InBlock.gifprivate bool _dataGridReadonly = false;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic StyleManager(DataGrid dataGrid)dot.gif{
InBlock.gif _dataGrid = dataGrid;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic StyleManager()dot.gif{;}
InBlock.gif
InBlock.gif [Category("Action")]
InBlock.gif [Description("可操作的数据表格")]
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic DataGrid @DataGriddot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gifgetdot.gif{return _dataGrid;}
ExpandedSubBlockStart.gifContractedSubBlock.gifsetdot.gif{
InBlock.gif _dataGrid=value;
InBlock.gif
InBlock.gifthis._dataGrid.CurrentCellChanged -= new System.EventHandler(this.dataGrid_CurrentCellChanged);
ExpandedSubBlockStart.gifContractedSubBlock.gifif(this._allowSelectedRow)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gifif(_dataGrid!=null)dot.gif{
InBlock.gif//修改事件
InBlock.gif
this._dataGrid.CurrentCellChanged += new System.EventHandler(this.dataGrid_CurrentCellChanged);
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gif [Category("Data")]
InBlock.gif [Description("要与数据表格绑定的数据源")]
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic DataTable DataSourcedot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gifgetdot.gif{return _dataTable;}
ExpandedSubBlockStart.gifContractedSubBlock.gifsetdot.gif{_dataTable =(value as DataTable);}
ExpandedSubBlockEnd.gif }
InBlock.gif [Category("Appearance")]
InBlock.gif [Description("获取当前的表格风格")]
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic DataGridTableStyle CurrentTableStyledot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gifgetdot.gif{return GetGridTableStyle();}
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 允许行选择模式
ExpandedSubBlockEnd.gif
/// </summary>
InBlock.gif [Category("Behavior")]
InBlock.gif [Description("允许行选择模式")]
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic bool AllowSelectedRowdot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gifgetdot.gif{return _allowSelectedRow;}
ExpandedSubBlockStart.gifContractedSubBlock.gifsetdot.gif{
InBlock.gif _allowSelectedRow =value;
InBlock.gifthis._dataGrid.CurrentCellChanged -= new System.EventHandler(this.dataGrid_CurrentCellChanged);
ExpandedSubBlockStart.gifContractedSubBlock.gifif(value)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gifif(_dataGrid!=null)dot.gif{
InBlock.gif//修改事件
InBlock.gif
this._dataGrid.CurrentCellChanged += new System.EventHandler(this.dataGrid_CurrentCellChanged);
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gif [Category("Behavior")]
InBlock.gif [Description("表格只读,也可隐藏底部的新增行")]
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic bool ReadOnlydot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gifgetdot.gif{return _dataGridReadonly;}
ExpandedSubBlockStart.gifContractedSubBlock.gifsetdot.gif{_dataGridReadonly = value;}
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 行选择事件
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="sender">对象</param>
ExpandedSubBlockEnd.gif
/// <param name="e">事件参数</param>
ExpandedSubBlockStart.gifContractedSubBlock.gifprivate void dataGrid_CurrentCellChanged(object sender, System.EventArgs e) dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifif(this.DataSource!=null)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.giftrydot.gif{
InBlock.gifthis._dataGrid.Select(this._dataGrid.CurrentRowIndex);
ExpandedSubBlockStart.gifContractedSubBlock.gifif(StyleManager.lastSelectIndex!=-1)dot.gif{
InBlock.gifthis._dataGrid.UnSelect(StyleManager.lastSelectIndex);
ExpandedSubBlockEnd.gif }
InBlock.gif StyleManager.lastSelectIndex = this._dataGrid.CurrentRowIndex;
ExpandedSubBlockStart.gifContractedSubBlock.gif }catchdot.gif{;}
ExpandedSubBlockEnd.gif }
InBlock.gif//锁定表格行的算法
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(isReadonlyRow&&this.ReadonlyRows!=null)dot.gif{
InBlock.gifthis._dataGrid.ReadOnly = false;
InBlock.gifif(this.ReadonlyRows.Length>0)
ExpandedSubBlockStart.gifContractedSubBlock.giffor(int i=0;i<ReadonlyRows.Length;i++)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gifif(this._dataGrid.CurrentRowIndex==ReadonlyRows[i])dot.gif{
InBlock.gifthis._dataGrid.ReadOnly = true;
InBlock.gifbreak;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gifif(_dataGridReadonly)dot.gif{
InBlock.gifthis._dataGrid.ReadOnly = true;
InBlock.gifreturn;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 设置列头风格
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="HeaderFont">字体</param>
InBlock.gif
/// <param name="HeaderFontColor">文字颜色</param>
ExpandedSubBlockEnd.gif
/// <returns>是否成功</returns>
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic bool SetHeader(Font HeaderFont,Color HeaderFontColor)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.giftrydot.gif{
InBlock.gif DataGridTableStyle currGTS = GetGridTableStyle();
ExpandedSubBlockStart.gifContractedSubBlock.gifif(currGTS==null)dot.gif{
InBlock.gifthrow new Exception("初始化表风格出错!");
ExpandedSubBlockEnd.gif }
InBlock.gif currGTS.HeaderFont = HeaderFont;
InBlock.gif currGTS.HeaderForeColor = HeaderFontColor;
InBlock.gifreturn true;
ExpandedSubBlockStart.gifContractedSubBlock.gif }catchdot.gif{
InBlock.gifreturn false;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 设置列头风格,默认黑色字体颜色
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="HeaderFont">字体</param>
ExpandedSubBlockEnd.gif
/// <returns>是否成功</returns>
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic bool SetHeader(Font HeaderFont)dot.gif{
InBlock.gifreturn SetHeader(HeaderFont,System.Drawing.Color.Black);
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 设置表格风格
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="CaptionText">标题文本</param>
InBlock.gif
/// <param name="CaptionVisible">标题可视</param>
InBlock.gif
/// <param name="RowHeaderVisible">行头可视</param>
ExpandedSubBlockEnd.gif
/// <returns>是否成功</returns>
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic bool SetGrid(string CaptionText,bool CaptionVisible,bool RowHeaderVisible)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.giftrydot.gif{
InBlock.gif DataGridTableStyle currGTS = GetGridTableStyle();
ExpandedSubBlockStart.gifContractedSubBlock.gifif(currGTS==null)dot.gif{
InBlock.gifthrow new Exception("初始化表风格出错!");
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gifif(this._dataGrid==null)dot.gif{
InBlock.gifthrow new Exception("无可操作的表格对象!");
ExpandedSubBlockEnd.gif }
InBlock.gifthis._dataGrid.CaptionText = CaptionText;
InBlock.gifthis._dataGrid.CaptionVisible = CaptionVisible;
InBlock.gif currGTS.RowHeadersVisible = RowHeaderVisible;
InBlock.gifreturn true;
ExpandedSubBlockStart.gifContractedSubBlock.gif }catchdot.gif{
InBlock.gifreturn false;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 根据数据类型获取列类型
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="tp">数据类型</param>
ExpandedSubBlockEnd.gif
/// <returns></returns>
ExpandedSubBlockStart.gifContractedSubBlock.gifprivate int getDataGridExColumn(Type tp)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gifif(tp==System.Type.GetType("System.Boolean"))dot.gif{
InBlock.gifreturn 2;
ExpandedSubBlockStart.gifContractedSubBlock.gif }else if(tp==System.Type.GetType("System.DateTime"))dot.gif{
InBlock.gifreturn 6;
ExpandedSubBlockStart.gifContractedSubBlock.gif }else if(tp==System.Type.GetType("System.Decimal"))dot.gif{
InBlock.gifreturn 1;
ExpandedSubBlockStart.gifContractedSubBlock.gif }else if(tp==System.Type.GetType("System.Single")||tp==System.Type.GetType("System.Double")) dot.gif{
InBlock.gifreturn 3;
ExpandedSubBlockStart.gifContractedSubBlock.gif }elsedot.gif{
InBlock.gifreturn 0;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 获取默认默认风格
InBlock.gif
/// </summary>
ExpandedSubBlockEnd.gif
/// <returns>表风格</returns>
ExpandedSubBlockStart.gifContractedSubBlock.gifprivate DataGridTableStyle GetGridTableStyle()dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.giftrydot.gif{
InBlock.gifif (_dataGrid.TableStyles.Count<=0&&_dataTable==null)
InBlock.gifthrow new ArgumentNullException("数据源为空!");
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifif((_dataGrid.DataSource as DataTable)!=_dataTable)dot.gif{
InBlock.gif _dataGrid.DataSource = _dataTable;
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifif(_dataGrid.TableStyles.Count<=0)dot.gif{
InBlock.gif DataGridTableStyle style = new DataGridTableStyle();
InBlock.gif style.MappingName = _dataTable.TableName;
ExpandedSubBlockStart.gifContractedSubBlock.gifforeach(DataColumn column in _dataTable.Columns) dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifif(column.DataType==System.Type.GetType("System.Boolean"))dot.gif{
InBlock.gif DataGridBoolColumn gridColumn = new DataGridBoolColumn();
InBlock.gif gridColumn.TrueValue = true;
InBlock.gif gridColumn.FalseValue = false;
InBlock.gif gridColumn.MappingName = column.ColumnName;
InBlock.gif gridColumn.HeaderText = column.ColumnName;
InBlock.gif gridColumn.Width = _dataGrid.PreferredColumnWidth;
InBlock.gif style.GridColumnStyles.Add(gridColumn);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif }elsedot.gif{
InBlock.gif DataGridTextBoxColumn gridColumn = new DataGridTextBoxColumn();
InBlock.gif gridColumn.MappingName = column.ColumnName;
InBlock.gif gridColumn.HeaderText = column.ColumnName;
InBlock.gif gridColumn.Width = _dataGrid.PreferredColumnWidth;
InBlock.gif gridColumn.NullText = "";
InBlock.gif style.GridColumnStyles.Add(gridColumn);
InBlock.gif
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gif _dataGrid.TableStyles.Clear();
InBlock.gif _dataGrid.TableStyles.Add(style);
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gifreturn _dataGrid.TableStyles[0];
ExpandedSubBlockStart.gifContractedSubBlock.gif }catchdot.gif{
InBlock.gifreturn null;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifprivate bool isBoolColumn(string ColumnName)dot.gif{
InBlock.gifreturn (_dataTable.Columns[ColumnName].DataType==System.Type.GetType("System.Boolean"));
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifprivate int getColumnIndex(DataGridTableStyle DGTS , string columName)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.giffor(int i=0;i<DGTS.GridColumnStyles.Count;i++)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gifif(DGTS.GridColumnStyles[i].HeaderText.ToString().Trim()==columName.ToString().Trim())dot.gif{
InBlock.gifreturn i;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gifreturn -1;
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 设置只读的行
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="rowIndexs">行索引数组</param>
InBlock.gif
/// <param name="Readonly">是否只读</param>
ExpandedSubBlockEnd.gif
/// <returns>是否成功</returns>
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic bool SetReadOnlyRows(int[] rowIndexs,bool Readonly)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.giftrydot.gif{
InBlock.gifthis.isReadonlyRow = Readonly;
InBlock.gifthis.ReadonlyRows = rowIndexs;
InBlock.gifreturn true;
ExpandedSubBlockStart.gifContractedSubBlock.gif }catchdot.gif{
InBlock.gifreturn false;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 设置列只读属性
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="columnIndexs">列索引数组</param>
InBlock.gif
/// <param name="Readonly">只读属性</param>
ExpandedSubBlockEnd.gif
/// <returns>是否处理成功</returns>
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic bool SetReadOnlyColumns(int[] columnIndexs,bool Readonly)dot.gif{
InBlock.gif//获取表风格
ExpandedSubBlockStart.gifContractedSubBlock.gif
trydot.gif{
InBlock.gif DataGridTableStyle currGTS = GetGridTableStyle();
ExpandedSubBlockStart.gifContractedSubBlock.gifif(currGTS==null)dot.gif{
InBlock.gifthrow new Exception("初始化表风格出错!");
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifif(columnIndexs==null||columnIndexs.Length<=0)dot.gif{
InBlock.gifthrow new ArgumentOutOfRangeException("只读索引超出范围!");
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif DataGridColumnStyle GCS;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.giffor(int i=0;i<columnIndexs.Length;i++)dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifif(columnIndexs[i]<0&&columnIndexs[i]>currGTS.GridColumnStyles.Count-1)dot.gif{
InBlock.gifthrow new ArgumentOutOfRangeException("索引超出范围!");
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif GCS = currGTS.GridColumnStyles[columnIndexs[i]];
ExpandedSubBlockStart.gifContractedSubBlock.gifif(GCS is DataGridBoolColumn)dot.gif{
InBlock.gif (GCS as DataGridBoolColumn).ReadOnly = Readonly;
ExpandedSubBlockStart.gifContractedSubBlock.gif }else if(GCS is DataGridTextBoxColumn)dot.gif{
InBlock.gif (GCS as DataGridTextBoxColumn).ReadOnly = Readonly;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gifreturn true;
ExpandedSubBlockStart.gifContractedSubBlock.gif }catchdot.gif{return false;}
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 设置列只读属性
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="columnNames">列名称数组</param>
InBlock.gif
/// <param name="Readonly">只读属性</param>
ExpandedSubBlockEnd.gif
/// <returns>是否处理成功</returns>
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic bool SetReadOnlyColumns(string[] columnNames,bool Readonly)dot.gif{
InBlock.gif//获取表风格
ExpandedSubBlockStart.gifContractedSubBlock.gif
trydot.gif{
InBlock.gif DataGridTableStyle currGTS = GetGridTableStyle();
ExpandedSubBlockStart.gifContractedSubBlock.gifif(currGTS==null)dot.gif{
InBlock.gifthrow new Exception("初始化表风格出错!");
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifif(columnNames==null||columnNames.Length<=0)dot.gif{
InBlock.gifthrow new ArgumentOutOfRangeException("只读索引超出范围!");
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif DataGridColumnStyle GCS;
InBlock.gifint columnIndex =-1;
ExpandedSubBlockStart.gifContractedSubBlock.giffor(int i=0;i<columnNames.Length;i++)dot.gif{
InBlock.gif columnIndex = getColumnIndex(currGTS,columnNames[i]);
ExpandedSubBlockStart.gifContractedSubBlock.gifif(columnIndex==-1)dot.gif{
InBlock.gifthrow new Exception("列名称为空或者无效!");
ExpandedSubBlockEnd.gif }
InBlock.gif GCS = currGTS.GridColumnStyles[columnNames[i]];
ExpandedSubBlockStart.gifContractedSubBlock.gifif(GCS is DataGridBoolColumn)dot.gif{
InBlock.gif (GCS as DataGridBoolColumn).ReadOnly = Readonly;
ExpandedSubBlockStart.gifContractedSubBlock.gif }else if(GCS is DataGridTextBoxColumn)dot.gif{
InBlock.gif (GCS as DataGridTextBoxColumn).ReadOnly = Readonly;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gifreturn true;
ExpandedSubBlockStart.gifContractedSubBlock.gif }catchdot.gif{return false;}
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 设置列属性
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="columnIndex">列索引值</param>
InBlock.gif
/// <param name="Width">宽度</param>
InBlock.gif
/// <param name="Title">标题文本</param>
InBlock.gif
/// <param name="Format">列格式</param>
InBlock.gif
/// <param name="Align">靠齐方式</param>
InBlock.gif
/// <param name="Visible">是否可见</param>
ExpandedSubBlockEnd.gif
/// <returns>完成状态</returns>
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic bool SetColumn(int columnIndex,int Width,string Title,FormatIndex Format,HorizontalAlignment Align,bool Visible)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.giftrydot.gif{
InBlock.gif//获取表风格
InBlock.gif
DataGridTableStyle currGTS = GetGridTableStyle();
InBlock.gifstring MappingName= null;
ExpandedSubBlockStart.gifContractedSubBlock.gifif(currGTS==null)dot.gif{
InBlock.gifthrow new Exception("初始化表风格出错!");
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifif(columnIndex<0&&columnIndex>currGTS.GridColumnStyles.Count-1)dot.gif{
InBlock.gifthrow new ArgumentOutOfRangeException("索引超出范围!");
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif//处理参数
InBlock.gif
Width = (Math.Abs(Width));
InBlock.gif Title = (Title == null ? "" : Title);
InBlock.gif Format = ((object)Format == null ? FormatIndex.Text : Format);
InBlock.gif Align = ((object)Align == null ? HorizontalAlignment.Left : Align);
InBlock.gif
InBlock.gif MappingName = currGTS.GridColumnStyles[columnIndex].MappingName;
InBlock.gif
InBlock.gif DataGridColumnStyle GCS = currGTS.GridColumnStyles[columnIndex];
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifif(GCS is DataGridTextBoxColumn)dot.gif{
InBlock.gif DataGridTextBoxColumn gridColumn = (GCS as DataGridTextBoxColumn);
ExpandedSubBlockStart.gifContractedSubBlock.gifswitch((int)Format)dot.gif{
InBlock.gifcase 2:
InBlock.gif gridColumn.Format = "C";
InBlock.gifbreak;
InBlock.gifcase 3:
InBlock.gif gridColumn.Format = "N";
InBlock.gifbreak;
InBlock.gifcase 5:
InBlock.gif gridColumn.Format=System.String.Format("yyyy年MM月dd日 hh:mm:ss",gridColumn.TextBox);
InBlock.gifbreak;
InBlock.gifcase 6:
InBlock.gif gridColumn.Format=System.String.Format("yyyy-MM-dd hh:mm:ss",gridColumn.TextBox);
InBlock.gifbreak;
InBlock.gifcase 7:
InBlock.gif gridColumn.Format=System.String.Format("yyyy-MM-dd",gridColumn.TextBox);
InBlock.gifbreak;
InBlock.gifcase 8:
InBlock.gif gridColumn.Format=System.String.Format("yy-MM-dd",gridColumn.TextBox);
InBlock.gifbreak;
InBlock.gifcase 9:
InBlock.gif gridColumn.Format= System.String.Format("hh:mm:ss",gridColumn.TextBox);
InBlock.gifbreak;
InBlock.gifdefault:
InBlock.gif gridColumn.Format = "";
InBlock.gifbreak;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif gridColumn.Width = Width;
InBlock.gif gridColumn.MappingName = MappingName;
InBlock.gif gridColumn.HeaderText = (Title.ToString()==""||Title==null ? MappingName : Title.ToString());
InBlock.gif gridColumn.Alignment = Align;
InBlock.gif gridColumn.NullText = "";
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif//隐藏字段
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(!Visible)dot.gif{
InBlock.gif GCS.Width =0;
InBlock.gif GCS.HeaderText ="";
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gifreturn true;
ExpandedSubBlockStart.gifContractedSubBlock.gif }catchdot.gif{
InBlock.gifreturn false;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 设置列属性,默认可见
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="columnIndex">列索引值</param>
InBlock.gif
/// <param name="Width">宽度</param>
InBlock.gif
/// <param name="Title">标题文本</param>
InBlock.gif
/// <param name="Format">列格式</param>
InBlock.gif
/// <param name="Align">靠齐方式</param>
ExpandedSubBlockEnd.gif
/// <returns>完成状态</returns>
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic bool SetColumn(int columnIndex,int Width,string Title,FormatIndex Format,HorizontalAlignment Align)dot.gif{
InBlock.gifreturn SetColumn(columnIndex,Width,Title,Format,Align,true);
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 设置列属性,默认可见,默认靠齐居左
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="columnIndex">列索引值</param>
InBlock.gif
/// <param name="Width">宽度</param>
InBlock.gif
/// <param name="Title">标题文本</param>
InBlock.gif
/// <param name="Format">列格式</param>
ExpandedSubBlockEnd.gif
/// <returns>完成状态</returns>
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic bool SetColumn(int columnIndex,int Width,string Title,FormatIndex Format)dot.gif{
InBlock.gifreturn SetColumn(columnIndex,Width,Title,Format,HorizontalAlignment.Left,true);
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 设置列属性,默认可见,默认靠齐居左,默认文本格式
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="columnIndex">列索引值</param>
InBlock.gif
/// <param name="Width">宽度</param>
InBlock.gif
/// <param name="Title">标题文本</param>
ExpandedSubBlockEnd.gif
/// <returns>完成状态</returns>
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic bool SetColumn(int columnIndex,int Width,string Title)dot.gif{
InBlock.gifreturn SetColumn(columnIndex,Width,Title,FormatIndex.Text,HorizontalAlignment.Left,true);
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 设置列属性,默认可见,默认靠齐居左,默认文本格式,默认标题为影射名
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="columnIndex">列索引值</param>
InBlock.gif
/// <param name="Width">宽度</param>
ExpandedSubBlockEnd.gif
/// <returns>完成状态</returns>
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic bool SetColumn(int columnIndex,int Width)dot.gif{
InBlock.gifreturn SetColumn(columnIndex,Width,"",FormatIndex.Text,HorizontalAlignment.Left,true);
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 设置列属性
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="columnName">列名称</param>
InBlock.gif
/// <param name="Width">宽度</param>
InBlock.gif
/// <param name="Title">标题文本</param>
InBlock.gif
/// <param name="Format">列格式</param>
InBlock.gif
/// <param name="Align">靠齐方式</param>
InBlock.gif
/// <param name="Visible">是否可见</param>
ExpandedSubBlockEnd.gif
/// <returns>完成状态</returns>
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic bool SetColumn(string columnName,int Width,string Title,FormatIndex Format,HorizontalAlignment Align,bool Visible)dot.gif{
InBlock.gif DataGridTableStyle currGTS = GetGridTableStyle();
ExpandedSubBlockStart.gifContractedSubBlock.gifif(currGTS==null)dot.gif{
InBlock.gifthrow new Exception("初始化表风格出错!");
ExpandedSubBlockEnd.gif }
InBlock.gifint columnIndex = getColumnIndex(currGTS,columnName);
ExpandedSubBlockStart.gifContractedSubBlock.gifif(columnIndex==-1)dot.gif{
InBlock.gifthrow new Exception("列名称为空或者无效!");
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gifreturn SetColumn(columnIndex,Width,Title,Format,Align,Visible);
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 设置列属性,默认可见
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="columnName">列名称</param>
InBlock.gif
/// <param name="Width">宽度</param>
InBlock.gif
/// <param name="Title">标题文本</param>
InBlock.gif
/// <param name="Format">列格式</param>
InBlock.gif
/// <param name="Align">靠齐方式</param>
ExpandedSubBlockEnd.gif
/// <returns>完成状态</returns>
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic bool SetColumn(string columnName,int Width,string Title,FormatIndex Format,HorizontalAlignment Align)dot.gif{
InBlock.gif DataGridTableStyle currGTS = GetGridTableStyle();
ExpandedSubBlockStart.gifContractedSubBlock.gifif(currGTS==null)dot.gif{
InBlock.gifthrow new Exception("初始化表风格出错!");
ExpandedSubBlockEnd.gif }
InBlock.gifint columnIndex = getColumnIndex(currGTS,columnName);
ExpandedSubBlockStart.gifContractedSubBlock.gifif(columnIndex==-1)dot.gif{
InBlock.gifthrow new Exception("列名称为空或者无效!");
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gifreturn SetColumn(columnIndex,Width,Title,Format,Align);
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 设置列属性,默认可见,默认靠齐居左
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="columnName">列名称</param>
InBlock.gif
/// <param name="Width">宽度</param>
InBlock.gif
/// <param name="Title">标题文本</param>
InBlock.gif
/// <param name="Format">列格式</param>
ExpandedSubBlockEnd.gif
/// <returns>完成状态</returns>
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic bool SetColumn(string columnName,int Width,string Title,FormatIndex Format)dot.gif{
InBlock.gif DataGridTableStyle currGTS = GetGridTableStyle();
ExpandedSubBlockStart.gifContractedSubBlock.gifif(currGTS==null)dot.gif{
InBlock.gifthrow new Exception("初始化表风格出错!");
ExpandedSubBlockEnd.gif }
InBlock.gifint columnIndex = getColumnIndex(currGTS,columnName);
ExpandedSubBlockStart.gifContractedSubBlock.gifif(columnIndex==-1)dot.gif{
InBlock.gifthrow new Exception("列名称为空或者无效!");
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gifreturn SetColumn(columnIndex,Width,Title,Format);
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 设置列属性,默认可见,默认靠齐居左,默认文本格式
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="columnName">列名称</param>
InBlock.gif
/// <param name="Width">宽度</param>
InBlock.gif
/// <param name="Title">标题文本</param>
ExpandedSubBlockEnd.gif
/// <returns>完成状态</returns>
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic bool SetColumn(string columnName,int Width,string Title)dot.gif{
InBlock.gif DataGridTableStyle currGTS = GetGridTableStyle();
ExpandedSubBlockStart.gifContractedSubBlock.gifif(currGTS==null)dot.gif{
InBlock.gifthrow new Exception("初始化表风格出错!");
ExpandedSubBlockEnd.gif }
InBlock.gifint columnIndex = getColumnIndex(currGTS,columnName);
ExpandedSubBlockStart.gifContractedSubBlock.gifif(columnIndex==-1)dot.gif{
InBlock.gifthrow new Exception("列名称为空或者无效!");
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gifreturn SetColumn(columnIndex,Width,Title);
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 设置列属性,默认可见,默认靠齐居左,默认文本格式,默认标题为影射名
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="columnName">列名称</param>
InBlock.gif
/// <param name="Width">宽度</param>
ExpandedSubBlockEnd.gif
/// <returns>完成状态</returns>
ExpandedSubBlockStart.gifContractedSubBlock.gifpublic bool SetColumn(string columnName,int Width)dot.gif{
InBlock.gif DataGridTableStyle currGTS = GetGridTableStyle();
ExpandedSubBlockStart.gifContractedSubBlock.gifif(currGTS==null)dot.gif{
InBlock.gifthrow new Exception("初始化表风格出错!");
ExpandedSubBlockEnd.gif }
InBlock.gifint columnIndex = getColumnIndex(currGTS,columnName);
ExpandedSubBlockStart.gifContractedSubBlock.gifif(columnIndex==-1)dot.gif{
InBlock.gifthrow new Exception("列名称为空或者无效!");
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gifreturn SetColumn(columnIndex,Width);
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedBlockEnd.gif}
None.gif
下面的代码是演示如何使用这个类:
None.gif 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" );
None.gif
None.gif OleDbDataAdapter da = new OleDbDataAdapter("SELECT A.设备编号, b.设备名称, b.所属设备组, A.检测时间, A.气体压力, A.换算压力, A.气体湿度, A.气体温度, A.诊断代码 FR" +
None.gif "OM (六氟化硫数据 A LEFT OUTER JOIN 测试设备 b ON A.设备编号 = b.编号) ORDER BY b.所属设备组, A.检测时间", conn );
None.gif DataSet ds = new DataSet();
ExpandedBlockStart.gif ContractedBlock.gif try dot.gif {
InBlock.gif da.Fill( ds, "六氟化硫数据" );
ExpandedBlockEnd.gif }
ExpandedBlockStart.gif ContractedBlock.gif catch( Exception ex ) dot.gif {
InBlock.gif MessageBox.Show( ex.Message );
ExpandedBlockEnd.gif }
None.gif
None.gif // 这里不需要制定,因为要由风格管理类接管,并且避免dataGrid自动初始化表风格影响速度
None.gif
// dataGrid.DataSource = ds.Tables[0];
None.gif

None.gif Grid.Service.StyleManager sm = new Grid.Service.StyleManager();
None.gif sm.DataGrid = dataGrid; // 目标DataGrid
None.gif
sm.DataSource = ds.Tables[0]; // 目标DataTable
None.gif
sm.AllowSelectedRow = true; // 允许行选择
None.gif
// 设置第1列宽200,名称为“设备编号!",数据类型为数值型,局中,不隐藏
None.gif
sm.SetColumn(0,200,"设备编号 !",Grid.Service.FormatIndex.Numeric,HorizontalAlignment.Center, true);
None.gif // 设置第2列宽100,不制定名称“用表字段真实名称”,文本类型
None.gif
sm.SetColumn(1,100,( string) null,Grid.Service.FormatIndex.Text,HorizontalAlignment.Center, true);
None.gif // 设置第4列为日期时间类型
None.gif
sm.SetColumn(3,200,( string) null,Grid.Service.FormatIndex.DateTime);
ExpandedBlockStart.gif ContractedBlock.gif sm.SetReadOnlyColumns( new int[] dot.gif {0,2,5}, true); // 只读列0,2,5
ExpandedBlockStart.gifContractedBlock.gif
sm.SetReadOnlyRows( new int[] dot.gif {0,4,6,8}, true); // 只读行0,4,6,8
None.gif
sm.SetHeader( new Font("黑体",9f),Color.Red); // 设置列头字体,黑体9号字,红色
None.gif
sm.SetGrid("测试表格", true, false); // 显示表格标题,不显示行标题
None.gif

None.gif conn.Close(



本文转自suifei博客园博客,原文链接http://www.cnblogs.com/Chinasf/archive/2005/04/18/139506.html,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值