码迷,mamicode.com
首页 > Windows程序 > 详细

C#重绘DataGridView行

时间:2016-05-20 08:34:07      阅读:622      评论:0      收藏:0      [点我收藏+]

标签:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Drawing;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Windows.Forms;
 7 
 8 namespace Metrox.WinFrom.DataGridView
 9 {
10     /// <summary>
11     /// 派生DataGridView
12     /// </summary>
13     class myDataGridView : DataGridView
14     {
15         public myDataGridView()
16         {
17             
18         }
19 
20         public myDataGridView(Form _form)
21         {
22             this.Dock = DockStyle.Fill;//占满停靠
23             this.RowHeadersVisible = false;//隐藏行头
24             this.ColumnHeadersVisible = true;//隐藏列头
25             this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;//列样式
26             this.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;//行样式
27             this.AllowUserToAddRows = false;//隐藏添加行
28             this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//选择模式
29             this.ReadOnly = true;//只读
30             this.MultiSelect = false;//单选
31             //奇数行样式
32             this.AlternatingRowsDefaultCellStyle = new DataGridViewCellStyle
33             {
34                 BackColor = Color.Silver,
35                 SelectionForeColor = Color.White
36             };
37             //行重绘事件
38             this.RowPrePaint += new DataGridViewRowPrePaintEventHandler(myDataGridView_RowPrePaint);
39             _form.Controls.Add(this);
40         }
41         /// <summary>
42         /// 重绘DataGridView行
43         /// </summary>
44         /// <param name="sender"></param>
45         /// <param name="e"></param>
46         private void myDataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
47         {
48             //要重绘的部份
49             e.PaintParts &= ~DataGridViewPaintParts.Focus;
50             //选中行时
51             if ((e.State & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected)
52             {
53                 //区域
54                 Rectangle rowBounds = new Rectangle(
55                     this.RowHeadersWidth, e.RowBounds.Top,
56                     this.Columns.GetColumnsWidth(
57                         DataGridViewElementStates.Visible) -
58                     this.HorizontalScrollingOffset + 1,
59                     e.RowBounds.Height);
60                 //重绘区域
61                 using (Brush backbrush =
62                     new System.Drawing.Drawing2D.LinearGradientBrush(rowBounds,
63                         this.DefaultCellStyle.SelectionBackColor,
64                         e.InheritedRowStyle.ForeColor,
65                         System.Drawing.Drawing2D.LinearGradientMode.Horizontal))
66                 {
67                     e.Graphics.FillRectangle(backbrush, rowBounds);
68                 }
69             }
70         }
71 
72     }
73 }

 

C#重绘DataGridView行

标签:

原文地址:http://www.cnblogs.com/linhongquan/p/5510822.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!