Question How to set borderstyle of a datagridview column at runtime (.Net Framework 4.0)?


Sep 18, 2011
Programming Experience
I have a readonly datagridview that is bound to a datasource. It has two columns. Now I want the first column to have no cell borderstyle; and the second one to have 'All' (i.e. all sides of the cell shall have a border) as cell borderstyle. Before binding the datagridview to the datasource, I'm writing something like mentioned below but it's taking no effect. Assume the column in question is named DisplayName.

DataGridViewAdvancedBorderStyle newStyle = New DataGridViewAdvancedBorderStyle();
newStyle.Top = DataGridViewAdvancedCellBorderStyle.Single;
newStyle.Left = DataGridViewAdvancedCellBorderStyle.Single;
newStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Single;
newStyle.Right = DataGridViewAdvancedCellBorderStyle.Single;

Please rectify or suggest a better way. Regards.
private void dgvLegends_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)

//Draw custom cell borders. //If current column is DisplayName...
if (dgvLegends.Columns("DisplayName").Index == e.ColumnIndex && e.RowIndex >= 0)
SolidBrush Brush = new SolidBrush(dgvLegends.ColumnHeadersDefaultCellStyle.BackColor); e.Graphics.FillRectangle(Brush, e.CellBounds); Brush.Dispose(); e.Paint(e.CellBounds, DataGridViewPaintParts.All & !DataGridViewPaintParts.ContentBackground);

ControlPaint.DrawBorder(e.Graphics, e.CellBounds, dgvLegends.GridColor, 1, ButtonBorderStyle.Solid, dgvLegends.GridColor, 1, ButtonBorderStyle.Solid, dgvLegends.GridColor, 1, ButtonBorderStyle.Solid, dgvLegends.GridColor, 1, ButtonBorderStyle.Solid);

e.Handled == true;

Latest posts

Top Bottom