设置datagridview中button按钮的背景颜色

无人久伴 提交于 2020-02-19 11:58:29

问题: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);
      }
}

效果:

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!