DataGridView重绘painting简单实例

时间:2020-12-14 11:16:33
private void dataGridViewX1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{

     if (e.RowIndex >=  && e.ColumnIndex>=)

     {

         Rectangle newRect = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width - , e.CellBounds.Height - );

         Pen borderPen = new Pen(dataGridViewX1.GridColor,);//线的颜色                 

         Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor);//非选中的背景色

         if (dataGridViewX1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected)

         {

             //选中的背景色

             backColorBrush = new SolidBrush(System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)())))));

         }

         e.Graphics.FillRectangle(backColorBrush, e.CellBounds);//用背景色填充单元格 

         if (e.ColumnIndex!=)

         {                      

             //画上边线

             e.Graphics.DrawLine(borderPen, e.CellBounds.Left, e.CellBounds.Top - , e.CellBounds.Right - , e.CellBounds.Top - );

             //画下边线

             e.Graphics.DrawLine(borderPen, e.CellBounds.Left, e.CellBounds.Bottom - , e.CellBounds.Right - , e.CellBounds.Bottom - );

             // 画右边线

             e.Graphics.DrawLine(borderPen, e.CellBounds.Right - , e.CellBounds.Top, e.CellBounds.Right - , e.CellBounds.Bottom - );                     

             //e.PaintBackground(newRect,true);//画背景      

             //e.Graphics.DrawRectangle(borderPen, newRect);//画边框  

         }                                

         else

         {                    

             if (e.RowIndex == )

             {

                 //头

                 e.Graphics.DrawLine(new Pen(Color.Red, ), e.CellBounds.Right - , (e.CellBounds.Top + e.CellBounds.Bottom) / , e.CellBounds.Right - , e.CellBounds.Bottom);

                 e.Graphics.DrawLine(new Pen(Color.Red, ), e.CellBounds.Right - , (e.CellBounds.Top + e.CellBounds.Bottom) / , e.CellBounds.Right, (e.CellBounds.Top + e.CellBounds.Bottom) / );

             }

             if (e.RowIndex == )

             {

                 //中

                 e.Graphics.DrawLine(new Pen(Color.Red, ), e.CellBounds.Right - , e.CellBounds.Top, e.CellBounds.Right - , e.CellBounds.Bottom + );

             }

             if (e.RowIndex == )

             {

                 //尾

                 e.Graphics.DrawLine(new Pen(Color.Red, ), e.CellBounds.Right - , e.CellBounds.Top, e.CellBounds.Right - , (e.CellBounds.Top + e.CellBounds.Bottom) / );

                 e.Graphics.DrawLine(new Pen(Color.Red, ), e.CellBounds.Right - , (e.CellBounds.Top + e.CellBounds.Bottom) / , e.CellBounds.Right, (e.CellBounds.Top + e.CellBounds.Bottom) / );

                 //画下边线

                 e.Graphics.DrawLine(borderPen, e.CellBounds.Left, e.CellBounds.Bottom - , e.CellBounds.Right - , e.CellBounds.Bottom - );

             }

             // 画右边线

             e.Graphics.DrawLine(borderPen, e.CellBounds.Right - , e.CellBounds.Top, e.CellBounds.Right - , e.CellBounds.Bottom - );                     

         }

         if (e.Value != null)

         {

             e.PaintContent(newRect);//画内容

         }

         e.Handled = true;

     }

 }

效果图:

DataGridView重绘painting简单实例

本文转载至,http://ruantnt.blog.163.com/blog/static/19052545220119185228153/