基于单个单元格值和自定义表格样式格式化 Datarow。





4.00/5 (11投票s)
2003年4月24日
2分钟阅读

124498

1949
一篇演示行格式化和向设计器添加自定义样式的文章。
引言
在本文中,我想向您展示如何根据该行中单个单元格的值更改网格中单个 DataRow
的格式。
此外,我将尝试解释将自定义样式添加到 Datagrid
集合编辑器的 Styles
集合的过程,以便您可以在设计时在 DataGrid
中使用它们。
您可以查看 http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q1020q 以获取有关此主题的更多信息。
使用代码
要根据单元格的值控制单行的格式,您必须执行以下操作
- 从
DataGridTextBoxColumn
派生。public class FormattedTextBoxColumn : DataGridTextBoxColumn { public CellPaint _handle ; public CellPaint PaintHandle { get { return _handle ; } set { _handle = value; } } ...; ...; }
- 重写
DataGridTextBox
列的 4 个绘制方法。protected override void Paint(Graphics g,Rectangle Bounds, CurrencyManager Source,int RowNum, Brush BackBrush ,Brush ForeBrush , bool AlignToRight) { Object data = ( Object ) GetColumnValueAtRow(Source, RowNum); String strData ; strData = data.ToString() ; FormatEventArgs e = new FormatEventArgs(RowNum) ; if( _handle != null) _handle(new object(),ref e) ; g.FillRectangle(e.BackBrush, Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height); FontStyle fs = FontStyle.Regular ; if( e.strikeThrough == true ) { fs = FontStyle.Strikeout ; } System.Drawing.Font font = new Font(System.Drawing.FontFamily.GenericSansSerif, (float)8.25 ,fs); g.DrawString( strData ,font ,Brushes.Green , Bounds.X ,Bounds.Y ); }
- 定义一个委托,从绘制函数(您重写的那些)调用
public delegate void CellPaint(object o, ref FormatEventArgs e);
- 定义一个从
EventArgs
派生的类,调用此FormatEventArgs
,它将用于将信息传递回委托。public class FormatEventArgs : EventArgs { ... ; ... ; }
要将自定义样式添加到设计器,您需要
- 创建一个自定义
Datagrid
,它从DataGrid
派生(用户定义的控件)public class CustomDataGrid : DataGrid { .... }
- 隐藏数据成员
GridTableStylesCollection
的DataGrid
,如下所示[Editor(typeof(CustomTableStylesCollectionEditor), typeof(UITypeEditor))] public new GridTableStylesCollection TableStyles { get{return base.TableStyles;} }
这是返回设计器中
Table Style
集合的数据成员。隐藏它,以便您可以返回自己的类型。 - 创建一个
internal
类,它从CollectionEditor
派生,并重写函数CreateNewItemTypes
,它现在将返回您的自定义样式。private class CustomTableStylesCollectionEditor : CollectionEditor { public CustomTableStylesCollectionEditor (Type type):base(type) { } //Over ride the function which will return //the STYLES collection protected override System.Type[] CreateNewItemTypes() { // refer to POINT 2 return new Type[] {typeof(CustomStylesCollection)}; } }
- 隐藏数据成员
- 创建一个自定义
DataGridTableStyle
,它从DataGridTableStyle
派生。public class CustomStylesCollection : DataGridTableStyle { ... ; ... ; }
- 现在,隐藏数据成员
GridColumnStylesCollection
,此成员显示设计时可用的默认ColumnStyles
。Editor(typeof(CustomColumnStylesCollectionEditor), typeof(UITypeEditor))] public new GridColumnStylesCollection GridColumnStyles { get {return base.GridColumnStyles;} }
并且,
protected override DataGridColumnStyle CreateGridColumn(PropertyDescriptor prop, bool isDefault) { return base.CreateGridColumn(prop, isDefault); }
- 创建一个
internal
类,它从CollectionEditor
派生,并重写函数CreateNewItemTypes
,它现在将返回您的自定义样式。private class CustomColumnStylesCollectionEditor : CollectionEditor { public CustomColumnStylesCollectionEditor(Type type) : base(type) { } protected override System.Type[] CreateNewItemTypes() { return new Type[] { typeof(FormattedTextBoxColumn), // Your Custom Style typeof(DataGridTextBoxColumn), // Default Style typeof(DataGridBoolColumn) // Default Style }; } }
- 现在,隐藏数据成员
构建项目后,CustomDataGrid
应该在您的工具箱中可用。
在您的 Form
应用程序中,放入此 CustomDataGrid
。然后使用设计器将列样式添加到 DataGrid
。
我尝试在属性框中公开 PaintHandler
,但由于某种原因,它不允许我在那里挂钩委托。
如果有人有任何关于如何做到这一点的想法,请告诉我。
现在,手动挂钩委托,如下所示
formattedTextBoxColumn1.PaintHandle += new CellPaint(PaintHandler) ;
并定义函数 PaintHandler
,它将允许对您的格式进行自定义处理。
public void PaintHandler( object o, ref FormatEventArgs e)
{
try
{
FormatEventArgs E = (FormatEventArgs) e ;
int rowNum = E.RowNum ;
DataRow dr = ds.Tables["table"].Rows[rowNum] ;
if( dr[0].ToString() == "X" || dr[0].ToString() == "x")
{
E.BackBrush = Brushes.Beige;
E.strikeThrough = true ;
}
else
{
E.BackBrush = Brushes.White ;
}
}
catch( System.SystemException )
{
}
}
您需要声明 DataGrid
的所有类型为 FormattedTextBoxColumn
的列样式,并将每个样式挂钩到该函数。
希望这有帮助。