用惯了Delphi第三方花哨的DataGrid控件,面对着VS2005的DataGridView中规中矩界面总觉得有点别扭.试着用了一下Developer Express Inc.NET()的DXperience.XtraGrid,发现跟同公司VCL版差不多,够强.不过感觉用了以后程序界面出现要慢得多了,而且部署时为了这一个Grid还得多带上好几M的Dll,真是不值得呀.
没办法,只好老老实实的想办法对DataGrid改造一下.毕竟DataGridView还是有很多不错的优点的.我不满意的地方也就是表头和没有间隔行背景.查MSDN,发现了行背景很容易就可以搞掂.在RowPrePaint里重画就行了,前提是要把RowTemplate.DefaultCellStyle.SelectionBackColor设为Color.Transparent.不过RowPrePaint里写的代码好像对表头没什么作用,郁闷...
再查MSDN,发现CellPainting 可以画表头,照着例子一试,背景是变了,不过文字,分隔线,排序标志什么的都没了.一度想放弃,最后还是在老多的论坛找到了解决方法:DataGridViewPaintParts.
提一下LinearGradientBrush,果然是好东西,很容易就是把渐变画出来了,够方便(Delphi里怎么就没有类似的呢...)
把上面提到的几点综合一下,就可以写出一个可以定义表头背景颜色,间隔行颜色和选中行颜色的DataGridView,当然要想用得方便,还是写成一个控件好用:
/**/
/****************************(如转载,请保留版权信息)************************
author:awin
e-mail:mrzwin@21cn.com
blog: http://blog.csdn.net/awin
*****************************(如转载,请保留版权信息)************************/
![]()
using
System.Drawing;
using
System.Drawing.Drawing2D;
using
System.Windows.Forms;
![]()
namespace
AwinControl.Windows.Forms
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
//部分代码只适用于Net2.0
[ToolboxBitmap(typeof (GridViewControl), "Bitmap.GridViewControl.bmp")] //定义在工具箱里显示图标,可以去掉
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// GridViewControl是一个可定义部分外观的DataGridView
/// 注意要想使得自画的背景有效,得把DataGridViewa相应的颜色属性设为Color.Transparent,这个在代码里没处理
/// </summary>
public class GridViewControl : DataGridView
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
private Color _ColumnHeaderColor1 = Color.White;
private Color _ColumnHeaderColor2 = Color.FromArgb(212, 208, 200);
private Color _SelectedRowColor1 = Color.White;
private Color _SelectedRowColor2 = Color.FromArgb(171, 217, 254);
private Color _PrimaryRowColor1 = Color.White;
private Color _PrimaryRowColor2 = Color.FromArgb(255, 249, 232);
private Color _SecondaryRowColor1 = Color.White;
private Color _SecondaryRowColor2 = Color.White;
private int _SecondaryLength = 2;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public Color ColumnHeaderColor1 //表头起始颜色
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
get ...{ return _ColumnHeaderColor1; }
set
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
_ColumnHeaderColor1 = value;
this.Invalidate();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public Color ColumnHeaderColor2 //表头终止颜色
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
get ...{ return _ColumnHeaderColor2; }
set
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
_ColumnHeaderColor2 = value;
this.Invalidate();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public Color PrimaryRowcolor1 //奇行起始颜色
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
get ...{ return _PrimaryRowColor1; }
set
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (value.IsEmpty || value == Color.Transparent)
_PrimaryRowColor1 = Color.White;
else
_PrimaryRowColor1 = value;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public Color PrimaryRowcolor2//奇行终止颜色
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
get ...{ return _PrimaryRowColor2; }
set
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (value.IsEmpty || value == Color.Transparent)
_PrimaryRowColor2 = Color.White;
else
_PrimaryRowColor2 = value;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public Color SecondaryRowColor1//偶行起始颜色
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
get ...{ return _SecondaryRowColor1; }
set
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (value.IsEmpty || value == Color.Transparent)
_SecondaryRowColor1 = Color.White;
else
_SecondaryRowColor1 = value;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public Color SecondaryRowColor2//偶行起始颜色
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
get ...{ return _SecondaryRowColor2; }
set
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (value.IsEmpty || value == Color.Transparent)
_SecondaryRowColor2 = Color.White;
else
_SecondaryRowColor2 = value;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public int SecondaryLength //这个长度现在是指导隔多少个行出现一个偶行
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
get ...{ return _SecondaryLength; }
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
set ...{ _SecondaryLength = value; }
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private void Init()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.RowPrePaint += new DataGridViewRowPrePaintEventHandler(this.GridView_RowPrePaint);
this.CellPainting += new DataGridViewCellPaintingEventHandler(this.GridView_CellPainting);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public GridViewControl()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Init();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public Color SelectedRowColor1 //选中行起始颜色
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
get ...{ return _SelectedRowColor1; }
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
set ...{ _SelectedRowColor1 = value; }
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public Color SelectedRowColor2 //选中行终止颜色
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
get ...{ return _SelectedRowColor2; }
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
set ...{ _SelectedRowColor2 = value; }
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static void DrawLinearGradient(Rectangle Rec, Graphics Grp, Color Color1, Color Color2)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (Color1 == Color2)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Brush backbrush = new SolidBrush(Color1);
Grp.FillRectangle(backbrush, Rec);
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
using (Brush backbrush =
new LinearGradientBrush(Rec, Color1, Color2,
LinearGradientMode.
Vertical))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Grp.FillRectangle(backbrush, Rec);
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private void GridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (e.RowIndex == -1)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (!(_ColumnHeaderColor1 == Color.Transparent) && !(_ColumnHeaderColor2 == Color.Transparent) &&
!_ColumnHeaderColor1.IsEmpty && !_ColumnHeaderColor2.IsEmpty)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
DrawLinearGradient(e.CellBounds, e.Graphics, _ColumnHeaderColor1, _ColumnHeaderColor2);
e.Paint(e.ClipBounds, (DataGridViewPaintParts.All & ~DataGridViewPaintParts.Background));
e.Handled = true;
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private
void GridView_RowPrePaint
(object
sender,
DataGridViewRowPrePaintEventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Rectangle rowBounds =
new Rectangle(0, e.RowBounds.Top, this.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) -
this.HorizontalScrollingOffset + 1,
e.RowBounds.Height);
e.PaintParts &= ~DataGridViewPaintParts.Focus;
if ((e.State & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (this.RowTemplate.DefaultCellStyle.SelectionBackColor == Color.Transparent)
DrawLinearGradient(rowBounds, e.Graphics, _SelectedRowColor1, _SelectedRowColor2);
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (this.RowTemplate.DefaultCellStyle.BackColor == Color.Transparent)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (e.RowIndex%_SecondaryLength == 1)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
DrawLinearGradient(rowBounds, e.Graphics, _PrimaryRowColor1, _PrimaryRowColor2);
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
DrawLinearGradient(rowBounds, e.Graphics, _SecondaryRowColor1, _SecondaryRowColor2);
}
}
}
}
}
;
}