自定义DataGridView外观

      用惯了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:[email protected]                                                   
 blog:  
http://blog.csdn.net/awin                                       
*****************************(如转载,请保留版权信息)***********************
*/


using  System.Drawing;
using  System.Drawing.Drawing2D;
using  System.Windows.Forms;

namespace  AwinControl.Windows.Forms
{
 
//部分代码只适用于Net2.0  
 [ToolboxBitmap(typeof (GridViewControl), "Bitmap.GridViewControl.bmp")] //定义在工具箱里显示图标,可以去掉
    /// 
    
/// GridViewControl是一个可定义部分外观的DataGridView
    
/// 注意要想使得自画的背景有效,得把DataGridViewa相应的颜色属性设为Color.Transparent,这个在代码里没处理
    
/// 

    public class GridViewControl : DataGridView
    
{
        
private Color _ColumnHeaderColor1 = Color.White;
        
private Color _ColumnHeaderColor2 = Color.FromArgb(212208200);
        
private Color _SelectedRowColor1 = Color.White;
        
private Color _SelectedRowColor2 = Color.FromArgb(171217254);
        
private Color _PrimaryRowColor1 = Color.White;
        
private Color _PrimaryRowColor2 = Color.FromArgb(255249232);
        
private Color _SecondaryRowColor1 = Color.White;
        
private Color _SecondaryRowColor2 = Color.White;
        
private int _SecondaryLength = 2;

        
public Color ColumnHeaderColor1 //表头起始颜色
        {
            
get return _ColumnHeaderColor1; }
            
set
            
{
                _ColumnHeaderColor1 
= value;
                
this.Invalidate();
            }

        }


        
public Color ColumnHeaderColor2 //表头终止颜色
        {
            
get return _ColumnHeaderColor2; }
            
set
            
{
                _ColumnHeaderColor2 
= value;
                
this.Invalidate();
            }

        }


        
public Color PrimaryRowcolor1 //奇行起始颜色
        {
            
get return _PrimaryRowColor1; }
            
set
            
{
                
if (value.IsEmpty || value == Color.Transparent)
                    _PrimaryRowColor1 
= Color.White;
                
else
                    _PrimaryRowColor1 
= value;
            }

        }


        
public Color PrimaryRowcolor2//奇行终止颜色
        {
            
get return _PrimaryRowColor2; }
            
set
            
{
                
if (value.IsEmpty || value == Color.Transparent)
                    _PrimaryRowColor2 
= Color.White;
                
else
                    _PrimaryRowColor2 
= value;
            }

        }


        
public Color SecondaryRowColor1//偶行起始颜色
        {
            
get return _SecondaryRowColor1; }
            
set
            
{
                
if (value.IsEmpty || value == Color.Transparent)
                    _SecondaryRowColor1 
= Color.White;
                
else
                    _SecondaryRowColor1 
= value;
            }

        }


        
public Color SecondaryRowColor2//偶行起始颜色
        {
            
get return _SecondaryRowColor2; }
            
set
            
{
                
if (value.IsEmpty || value == Color.Transparent)
                    _SecondaryRowColor2 
= Color.White;
                
else
                    _SecondaryRowColor2 
= value;
            }

        }


        
public int SecondaryLength //这个长度现在是指导隔多少个行出现一个偶行
        {
            
get return _SecondaryLength; }
            
set { _SecondaryLength = value; }
        }


        
private void Init()
        
{
            
this.RowPrePaint += new DataGridViewRowPrePaintEventHandler(this.GridView_RowPrePaint);
            
this.CellPainting += new DataGridViewCellPaintingEventHandler(this.GridView_CellPainting);
        }


        
public GridViewControl()
        
{
            Init();
        }


        
public Color SelectedRowColor1 //选中行起始颜色
        {
            
get return _SelectedRowColor1; }

            
set { _SelectedRowColor1 = value; }
        }


        
public Color SelectedRowColor2 //选中行终止颜色
        {
            
get return _SelectedRowColor2; }

            
set { _SelectedRowColor2 = value; }
        }


        
private static void DrawLinearGradient(Rectangle Rec, Graphics Grp, Color Color1, Color Color2)
        
{
            
if (Color1 == Color2)
            
{
                Brush backbrush 
= new SolidBrush(Color1);
                Grp.FillRectangle(backbrush, Rec);
            }

            
else
            
{
                
using (Brush backbrush =
                    
new LinearGradientBrush(Rec, Color1, Color2,
                                            LinearGradientMode.
                                                Vertical))
                
{
                    Grp.FillRectangle(backbrush, Rec);
                }

            }

        }


        
private void GridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        
{
            
if (e.RowIndex == -1)
            
{
                
if (!(_ColumnHeaderColor1 == Color.Transparent) && !(_ColumnHeaderColor2 == Color.Transparent) &&
                    
!_ColumnHeaderColor1.IsEmpty && !_ColumnHeaderColor2.IsEmpty)
                
{
                    DrawLinearGradient(e.CellBounds, e.Graphics, _ColumnHeaderColor1, _ColumnHeaderColor2);
                    e.Paint(e.ClipBounds, (DataGridViewPaintParts.All 
& ~DataGridViewPaintParts.Background));
                    e.Handled 
= true;
                }

            }

        }


        
private
            
void GridView_RowPrePaint
            (
object
                 sender,
             DataGridViewRowPrePaintEventArgs e)
        
{
            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)
            
{
                
if (this.RowTemplate.DefaultCellStyle.SelectionBackColor == Color.Transparent)
                    DrawLinearGradient(rowBounds, e.Graphics, _SelectedRowColor1, _SelectedRowColor2);
            }

            
else
            
{
                
if (this.RowTemplate.DefaultCellStyle.BackColor == Color.Transparent)
                
{
                    
if (e.RowIndex%_SecondaryLength == 1)
                    
{
                        DrawLinearGradient(rowBounds, e.Graphics, _PrimaryRowColor1, _PrimaryRowColor2);
                    }

                    
else
                    
{
                        DrawLinearGradient(rowBounds, e.Graphics, _SecondaryRowColor1, _SecondaryRowColor2);
                    }

                }

            }

        }

    }

        ;
}

 

 

你可能感兴趣的:(C#)