設置datagridview中button按鈕的背景顏色


問題:DataGridViewButtonColumn()在datagridview中創建按鈕列,如何設置按鈕的背景顏色(不是單元格的背景顏色)。

回答:可以在dataGridView1_CellPainting事件里面處理。

 

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
      if (e.ColumnIndex == 0)//索引0
      {
           e.Handled = true;
 
           using (SolidBrush brush = new SolidBrush(Color.Red))
           {
                e.Graphics.FillRectangle(brush, e.CellBounds);
           }
           ControlPaint.DrawBorder(e.Graphics, e.CellBounds, Color.Yellow, ButtonBorderStyle.Outset);
      }
      if (e.ColumnIndex == 1)//索引1
      {
           e.Handled = true;
 
           using (SolidBrush brush = new SolidBrush(Color.BlueViolet))
           {
                e.Graphics.FillRectangle(brush, e.CellBounds);
           }
           ControlPaint.DrawBorder(e.Graphics, e.CellBounds, Color.Yellow, ButtonBorderStyle.Outset);
      }
}

效果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM