Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a border color on a Windows Form

Does anybody know how to change the border color of a datagridview in a windows form?

like image 999
yeahumok Avatar asked Feb 01 '10 14:02

yeahumok


1 Answers

You can't, it is drawn with the color that the user selected in her preferred theme, selected in Control Panel's Display applet. Overriding the user preference is risky, but you can do so by drawing it yourself. Set the DGV's BorderStyle property to None and draw a border yourself in the form's OnPaintBackground() method. For example:

protected override void OnPaintBackground(PaintEventArgs e) {
  base.OnPaintBackground(e);
  Rectangle rc = new Rectangle(dataGridView1.Left - 1, dataGridView1.Top - 1,
    dataGridView1.Size.Width + 1, dataGridView1.Size.Height + 1);
  e.Graphics.DrawRectangle(Pens.Fuchsia, rc);
}
like image 168
Hans Passant Avatar answered Oct 04 '22 11:10

Hans Passant