DataGridView二维表头与合并单元格.docx
《DataGridView二维表头与合并单元格.docx》由会员分享,可在线阅读,更多相关《DataGridView二维表头与合并单元格.docx(22页珍藏版)》请在冰豆网上搜索。
DataGridView二维表头与合并单元格
DataGridView二维表头与合并单元格
1、在项目中添加“组件类”。
2、所引用的命名空间如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
usingSystem.Drawing;
usingSystem.Drawing.Design;
usingSystem.Diagnostics;
3、DataGridView二维表头与合并单元格类,继承DataGridView类
publicpartialclassHeaderUnitView:
DataGridView
{
privateTreeView[]_columnTreeView;
privateArrayList_columnList=newArrayList();
privateint_cellHeight=17;
publicintCellHeight
{
get{return_cellHeight;}
set{_cellHeight=value;}
}
privateint_columnDeep=1;
privateboolHscrollRefresh=false;
///
///水平滚动时是否刷新表头,数据较多时可能会闪烁,不刷新时可能显示错误
///
[Description("水平滚动时是否刷新表头,数据较多时可能会闪烁,不刷新时可能显示错误")]
publicboolRefreshAtHscroll
{
get{returnHscrollRefresh;}
set{HscrollRefresh=value;}
}
///
///构造函数
///
publicHeaderUnitView()
{
InitializeComponent();
this.ColumnHeadersHeightSizeMode=DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
//设置列高度显示模式
}
publicHeaderUnitView(IContainercontainer)
{
container.Add(this);
InitializeComponent();
}
[Description("设置或获得合并表头树的深度")]
publicintColumnDeep
{
get
{
if(this.Columns.Count==0)
_columnDeep=1;
this.ColumnHeadersHeight=_cellHeight*_columnDeep;
return_columnDeep;
}
set
{
if(value<1)
_columnDeep=1;
else
_columnDeep=value;
this.ColumnHeadersHeight=_cellHeight*_columnDeep;
}
}
[Description("添加合并式单元格绘制的所需要的节点对象")]
publicTreeView[]ColumnTreeView
{
get{return_columnTreeView;}
set
{
if(_columnTreeView!
=null)
{
for(inti=0;i<=_columnTreeView.Length-1;i++)
_columnTreeView[i].Dispose();
}
_columnTreeView=value;
}
}
[Description("设置添加的字段树的相关属性")]
publicTreeViewColumnTreeViewNode
{
get{return_columnTreeView[0];}
}
///
///设置或获取合并列的集合
///
[MergableProperty(false)]
[Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor,System.Design,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a",typeof(UITypeEditor))]
[DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Visible)]
[Localizable(true)]
[Description("设置或获取合并列的集合"),Browsable(true),Category("单元格合并")]
publicListMergeColumnNames
{
get
{
return_mergecolumnname;
}
set
{
_mergecolumnname=value;
}
}
privateList_mergecolumnname=newList();
publicArrayListNadirColumnList
{
get
{
if(_columnTreeView==null)
returnnull;
if(_columnTreeView[0]==null)
returnnull;
if(_columnTreeView[0].Nodes==null)
returnnull;
if(_columnTreeView[0].Nodes.Count==0)
returnnull;
_columnList.Clear();
GetNadirColumnNodes(_columnList,_columnTreeView[0].Nodes[0],false);
return_columnList;
}
}
///
///绘制合并表头
///
///合并表头节点
///绘图参数集
///结点深度
///
publicvoidPaintUnitHeader(
TreeNodenode,
System.Windows.Forms.DataGridViewCellPaintingEventArgse,
intlevel)
{
//根节点时退出递归调用
if(level==0)
return;
RectangleFuhRectangle;
intuhWidth;
SolidBrushgridBrush=newSolidBrush(this.GridColor);
SolidBrushbackColorBrush=newSolidBrush(e.CellStyle.BackColor);
PengridLinePen=newPen(gridBrush);
StringFormattextFormat=newStringFormat();
textFormat.Alignment=StringAlignment.Center;
uhWidth=GetUnitHeaderWidth(node);
if(node.Nodes.Count==0)
{
uhRectangle=newRectangle(e.CellBounds.Left,
e.CellBounds.Top+node.Level*_cellHeight,
uhWidth-1,
_cellHeight*(_columnDeep-node.Level)-1);
}
else
{
uhRectangle=newRectangle(
e.CellBounds.Left,
e.CellBounds.Top+node.Level*_cellHeight,
uhWidth-1,
_cellHeight-1);
}
//画矩形
e.Graphics.FillRectangle(backColorBrush,uhRectangle);
//划底线
e.Graphics.DrawLine(gridLinePen
,uhRectangle.Left
,uhRectangle.Bottom
,uhRectangle.Right
,uhRectangle.Bottom);
//划右端线
e.Graphics.DrawLine(gridLinePen
,uhRectangle.Right
,uhRectangle.Top
,uhRectangle.Right
,uhRectangle.Bottom);
////写字段文本
e.Graphics.DrawString(node.Text,this.Font
,newSolidBrush(e.CellStyle.ForeColor)
,uhRectangle.Left+uhRectangle.Width/2-
e.Graphics.MeasureString(node.Text,this.Font).Width/2-1
,uhRectangle.Top+
uhRectangle.Height/2-e.Graphics.MeasureString(node.Text,this.Font).Height/2);
//递归调用()
if(node.PrevNode==null)
if(node.Parent!
=null)
PaintUnitHeader(node.Parent,e,level-1);
}
///
///获得合并标题字段的宽度
///
///字段节点
///字段宽度
///
privateintGetUnitHeaderWidth(TreeNodenode)
{
//获得非最底层字段的宽度
intuhWidth=0;
//获得最底层字段的宽度
if(node.Nodes==null)
returnthis.Columns[GetColumnListNodeIndex(node)].Width;
if(node.Nodes.Count==0)
returnthis.Columns[GetColumnListNodeIndex(node)].Width;
for(inti=0;i<=node.Nodes.Count-1;i++)
{
uhWidth=uhWidth+GetUnitHeaderWidth(node.Nodes[i]);
}
returnuhWidth;
}
///
///获得底层字段索引
///
///'底层字段节点
///索引
///
privateintGetColumnListNodeIndex(TreeNodenode)
{
for(inti=0;i<=_columnList.Count-1;i++)
{
if(((TreeNode)_columnList[i]).Equals(node))
returni;
}
return-1;
}
///
///获得底层字段集合
///
///底层字段集合
///字段节点
///向上搜索与否
///
privatevoidGetNadirColumnNodes(
ArrayListalList,
TreeNodenode,
BooleanisChecked)
{
if(isChecked==false)
{
if(node.FirstNode==null)
{
alList.Add(node);
if(node.NextNode!
=null)
{
GetNadirColumnNodes(alList,node.NextNode,false);
return;
}
if(node.Parent!
=null)
{
GetNadirColumnNodes(alList,node.Parent,true);
return;
}
}
else
{
if(node.FirstNode!
=null)
{
GetNadirColumnNodes(alList,node.FirstNode,false);
return;
}
}
}
else
{
if(node.FirstNode==null)
{
return;
}
else
{
if(node.NextNode!
=null)
{
GetNadirColumnNode