DataGridView重繪painting簡單一實例

來源:互聯網
上載者:User

標籤:datagridview   style   class   blog   c   code   

private void dataGridViewX1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e){     if (e.RowIndex >= 0 && e.ColumnIndex>=0)     {         Rectangle newRect = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width - 1, e.CellBounds.Height - 1);         Pen borderPen = new Pen(dataGridViewX1.GridColor,1);//線的顏色                          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)(255)))), ((int)(((byte)(199)))), ((int)(((byte)(118))))));         }         e.Graphics.FillRectangle(backColorBrush, e.CellBounds);//用背景色填充儲存格          if (e.ColumnIndex!=1)         {                                   //畫上邊線             e.Graphics.DrawLine(borderPen, e.CellBounds.Left, e.CellBounds.Top - 1, e.CellBounds.Right - 1, e.CellBounds.Top - 1);             //畫下邊線             e.Graphics.DrawLine(borderPen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);             // 畫右邊線             e.Graphics.DrawLine(borderPen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);                                  //e.PaintBackground(newRect,true);//畫背景                   //e.Graphics.DrawRectangle(borderPen, newRect);//畫邊框           }                                         else         {                                 if (e.RowIndex == 0)             {                 //頭                 e.Graphics.DrawLine(new Pen(Color.Red, 4), e.CellBounds.Right - 2, (e.CellBounds.Top + e.CellBounds.Bottom) / 2, e.CellBounds.Right - 2, e.CellBounds.Bottom);                 e.Graphics.DrawLine(new Pen(Color.Red, 3), e.CellBounds.Right - 8, (e.CellBounds.Top + e.CellBounds.Bottom) / 2, e.CellBounds.Right, (e.CellBounds.Top + e.CellBounds.Bottom) / 2);             }             if (e.RowIndex == 1)             {                 //中                 e.Graphics.DrawLine(new Pen(Color.Red, 4), e.CellBounds.Right - 2, e.CellBounds.Top, e.CellBounds.Right - 2, e.CellBounds.Bottom + 1);             }             if (e.RowIndex == 2)             {                 //尾                 e.Graphics.DrawLine(new Pen(Color.Red, 4), e.CellBounds.Right - 2, e.CellBounds.Top, e.CellBounds.Right - 2, (e.CellBounds.Top + e.CellBounds.Bottom) / 2);                 e.Graphics.DrawLine(new Pen(Color.Red, 3), e.CellBounds.Right - 8, (e.CellBounds.Top + e.CellBounds.Bottom) / 2, e.CellBounds.Right, (e.CellBounds.Top + e.CellBounds.Bottom) / 2);                 //畫下邊線                 e.Graphics.DrawLine(borderPen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);             }             // 畫右邊線             e.Graphics.DrawLine(borderPen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);                              }         if (e.Value != null)         {             e.PaintContent(newRect);//畫內容         }         e.Handled = true;     } }

本文轉載至,http://ruantnt.blog.163.com/blog/static/19052545220119185228153/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.