众所周知,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
{
/**/ ///
1<summary>
2 /// 格式化字符串
3 /// </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)
} ;
/**/ ///
1<summary>
2 /// StyleManager 的摘要说明。
3 /// DataGrid 的属性管理类
4 /// </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();}
}
/**/ ///
1<summary>
2 /// 允许行选择模式
3 /// </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;}
}
/**/ ///
1<summary>
2 /// 行选择事件
3 /// </summary>
///
1<param name="sender"/>
对象 ///
1<param name="e"/>
事件参数
private void dataGrid_CurrentCellChanged( object sender, System.EventArgs e)
{
if ( this .DataSource != null )
{
try
{