DataGrid 风格管理类测试版源码 - 中国WEB开发者网络 (http://www.webasp.net) -- 技术教程 (http://www.webasp.net/article/) --- DataGrid 风格管理类测试版源码 (http://www.webasp.net/article/27/26951.htm) |
| -- 作者:未知 -- 发布日期: 2005-10-22 |
| 众所周知,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); 现在我把类的源码贴上面,我不保证代码的一些做法是否合理与正确,仅供参考: namespace Grid.Service { /**//// <summary> public enum FormatIndex { /**//// <summary> public class StyleManager { public StyleManager(DataGrid dataGrid) { public StyleManager() {;} public DataGrid @DataGrid { get {return _dataGrid;} set { if(this._allowSelectedRow) { if(_dataGrid!=null) { public DataTable DataSource { get {return _dataTable;} set {_dataTable =(value as DataTable);} public DataGridTableStyle CurrentTableStyle { get {return GetGridTableStyle();} /**//// <summary> public bool AllowSelectedRow { get {return _allowSelectedRow;} set { if(value) { if(_dataGrid!=null) { public bool ReadOnly { get {return _dataGridReadonly;} set {_dataGridReadonly = value;} /**//// <summary> private void dataGrid_CurrentCellChanged(object sender, System.EventArgs e) { if(this.DataSource!=null) { try { if(StyleManager.lastSelectIndex!=-1) { }catch {;} if(isReadonlyRow&&this.ReadonlyRows!=null) { for(int i=0;i<ReadonlyRows.Length;i++) { if(this._dataGrid.CurrentRowIndex==ReadonlyRows[i]) { if(_dataGridReadonly) { /**//// <summary> public bool SetHeader(Font HeaderFont,Color HeaderFontColor) { try { if(currGTS==null) { }catch { /**//// <summary> public bool SetHeader(Font HeaderFont) { /**//// <summary> public bool SetGrid(string CaptionText,bool CaptionVisible,bool RowHeaderVisible) { try { if(currGTS==null) { if(this._dataGrid==null) { }catch { /**//// <summary> private int getDataGridExColumn(Type tp) { if(tp==System.Type.GetType("System.Boolean")) { }else if(tp==System.Type.GetType("System.DateTime")) { }else if(tp==System.Type.GetType("System.Decimal")) { }else if(tp==System.Type.GetType("System.Single")||tp==System.Type.GetType("System.Double")) { }else { /**//// <summary> private DataGridTableStyle GetGridTableStyle() { try { if((_dataGrid.DataSource as DataTable)!=_dataTable) { if(_dataGrid.TableStyles.Count<=0) { foreach(DataColumn column in _dataTable.Columns) { if(column.DataType==System.Type.GetType("System.Boolean")) { |