【转】DataGridView 的重绘

 .net Winform 中的DataGridView 的外观有点难看。下面是重绘DataGridView的方法,可以重绘时参考:

1.重绘DataGridView 

[csharp]  view plain copy
  1. protected override void Paint(  
  2.     Graphics graphics,  
  3.     Rectangle clipBounds,  
  4.     Rectangle cellBounds,  
  5.     int rowIndex,  
  6.     DataGridViewElementStates cellState,  
  7.     object value,  
  8.     object formattedValue,  
  9.     string errorText,  
  10.     DataGridViewCellStyle cellStyle,  
  11.     DataGridViewAdvancedBorderStyle advancedBorderStyle,  
  12.     DataGridViewPaintParts paintParts)  
  13. {  
  14.     // Call the base class method to paint the default cell appearance.  
  15.     base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,  
  16.         value, formattedValue, errorText, cellStyle,  
  17.         advancedBorderStyle, paintParts);  
  18.   
  19.     // Retrieve the client location of the mouse pointer.  
  20.     Point cursorPosition =  
  21.         this.DataGridView.PointToClient(Cursor.Position);  
  22.   
  23.     // If the mouse pointer is over the current cell, draw a custom border.  
  24.     if (cellBounds.Contains(cursorPosition))  
  25.     {  
  26.         Rectangle newRect = new Rectangle(cellBounds.X + 1,  
  27.             cellBounds.Y + 1, cellBounds.Width - 4,  
  28.             cellBounds.Height - 4);  
  29.         graphics.DrawRectangle(Pens.Red, newRect);  
  30.     }  
  31. }  

2.重绘DataGridView 的头部

[csharp]  view plain copy
  1. protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)  
  2.         {  
  3.             base.OnCellPainting(e);  
  4.   
  5.             if (e.ColumnIndex == -1 && e.RowIndex == -1)  
  6.             {  
  7.                 using (LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, Color.LightGray,  
  8.                     Color.White, LinearGradientMode.ForwardDiagonal))  
  9.                 {  
  10.                     e.Graphics.FillRectangle(brush, e.CellBounds);  
  11.                     Rectangle border = e.CellBounds;  
  12.                     border.Offset(new Point(-1, -1));  
  13.                     e.Graphics.DrawRectangle(Pens.Gray, border);  
  14.                 }  
  15.                 e.PaintContent(e.CellBounds);  
  16.                 e.Handled = true;  
  17.             }  
  18.             else if (e.RowIndex == -1)  
  19.             {  
  20.                 //标题行  
  21.                 using (LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, Color.LightGray,  
  22.                     Color.White, LinearGradientMode.Vertical))  
  23.                 {  
  24.                     e.Graphics.FillRectangle(brush, e.CellBounds);  
  25.                     Rectangle border = e.CellBounds;  
  26.                     border.Offset(new Point(-1, -1));  
  27.                     e.Graphics.DrawRectangle(Pens.Gray, border);  
  28.                 }  
  29.                 e.PaintContent(e.CellBounds);  
  30.                 e.Handled = true;  
  31.             }  
  32.             else if (e.ColumnIndex == -1)  
  33.             {  
  34.                 //标题列  
  35.                 using (LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, Color.LightGray,  
  36.                     Color.White, LinearGradientMode.Horizontal))  
  37.                 {  
  38.                     e.Graphics.FillRectangle(brush, e.CellBounds);  
  39.                     Rectangle border = e.CellBounds;  
  40.                     border.Offset(new Point(-1, -1));  
  41.                     e.Graphics.DrawRectangle(Pens.Gray, border);  
  42.                 }  
  43.                 e.PaintContent(e.CellBounds);  
  44.                 e.Handled = true;  
  45.             }  
  46.         }  

3.重绘实现合并单元格

[csharp]  view plain copy
  1. #region"合并单元格的测试"  
  2. private int? nextrow = null;  
  3. private int? nextcol = null;  
  4. private void dataGridView1_CellFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)  
  5. {  
  6.     if (this.dataGridView1.Columns["description"].Index == e.ColumnIndex && e.RowIndex >= 0)  
  7.     {  
  8.         if (this.nextcol != null & e.ColumnIndex == this.nextcol)  
  9.         {  
  10.             e.CellStyle.BackColor = Color.LightBlue;  
  11.             this.nextcol = null;  
  12.         }  
  13.         if (this.nextrow != null & e.RowIndex == nextrow)  
  14.         {  
  15.             e.CellStyle.BackColor = Color.LightPink;  
  16.             this.nextrow = null;  
  17.         }  
  18.         if (e.RowIndex != this.dataGridView1.RowCount - 1)  
  19.         {  
  20.             if (e.Value.ToString() == this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())  
  21.             {  
  22.                 e.CellStyle.BackColor = Color.LightPink;  
  23.                 nextrow = e.RowIndex + 1;  
  24.             }  
  25.         }  
  26.     }  
  27.     if (this.dataGridView1.Columns["name"].Index == e.ColumnIndex && e.RowIndex >= 0)  
  28.     {  
  29.         if (e.Value.ToString() == this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString())  
  30.         {  
  31.             e.CellStyle.BackColor = Color.LightBlue;  
  32.             nextcol = e.ColumnIndex + 1;  
  33.         }  
  34.     }  
  35. }  
  36. //==========================  
  37.          
  38. //绘制单元格  
  39. private void dataGridView1_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)  
  40. {  
  41.   
  42.    
  43.   
  44.     //纵向合并  
  45.     if (this.dataGridView1.Columns["description"].Index == e.ColumnIndex && e.RowIndex >= 0)  
  46.     {  
  47.   
  48.         using (  
  49.             Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),  
  50.             backColorBrush = new SolidBrush(e.CellStyle.BackColor))  
  51.         {  
  52.             using (Pen gridLinePen = new Pen(gridBrush))  
  53.             {  
  54.                 // 擦除原单元格背景  
  55.                 e.Graphics.FillRectangle(backColorBrush, e.CellBounds);  
  56.                 绘制线条,这些线条是单元格相互间隔的区分线条,  
  57.                 因为我们只对列name做处理,所以datagridview自己会处理左侧和上边缘的线条  
  58.                 if (e.RowIndex != this.dataGridView1.RowCount - 1)  
  59.                 {  
  60.                     if (e.Value.ToString() != this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())  
  61.                     {  
  62.   
  63.                         e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,  
  64.                         e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线  
  65.                         //绘制值  
  66.                         if (e.Value != null)  
  67.                         {  
  68.                             e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,  
  69.                                 Brushes.Crimson, e.CellBounds.X + 2,  
  70.                                 e.CellBounds.Y + 2, StringFormat.GenericDefault);  
  71.                         }  
  72.                     }  
  73.                 }  
  74.                 else  
  75.                 {  
  76.                     e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,  
  77.                         e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线  
  78.                     //绘制值  
  79.                     if (e.Value != null)  
  80.                     {  
  81.                         e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,  
  82.                             Brushes.Crimson, e.CellBounds.X + 2,  
  83.                             e.CellBounds.Y + 2, StringFormat.GenericDefault);  
  84.                     }  
  85.                 }  
  86.                 //右侧的线  
  87.                 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,  
  88.                     e.CellBounds.Top, e.CellBounds.Right - 1,  
  89.                     e.CellBounds.Bottom - 1);  
  90.   
  91.                 e.Handled = true;  
  92.             }  
  93.         }  
  94.     }  
  95.   
  96.     //横向合并  
  97.     if (this.dataGridView1.Columns["name"].Index == e.ColumnIndex && e.RowIndex >= 0)  
  98.     {  
  99.   
  100.         using (  
  101.             Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),  
  102.             backColorBrush = new SolidBrush(e.CellStyle.BackColor))  
  103.         {  
  104.             using (Pen gridLinePen = new Pen(gridBrush))  
  105.             {  
  106.                 // 擦除原单元格背景  
  107.                 e.Graphics.FillRectangle(backColorBrush, e.CellBounds);  
  108.   
  109.                 if (e.Value.ToString() != this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString())  
  110.                 {  
  111.   
  112.                     //右侧的线  
  113.                     e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top,  
  114.                         e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);  
  115.                     //绘制值  
  116.                     if (e.Value != null)  
  117.                     {  
  118.                         e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,  
  119.                             Brushes.Crimson, e.CellBounds.X + 2,  
  120.                             e.CellBounds.Y + 2, StringFormat.GenericDefault);  
  121.                     }  
  122.                 }  
  123.   
  124.                 //下边缘的线  
  125.                 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,  
  126.                                             e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);  
  127.                 e.Handled = true;  
  128.             }  
  129.         }  
  130.   
  131.     }  
  132.   
  133. }  
  134.  
  135. #endregion  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值