1

I would like to do this border style on my 3rd column's cells.
Can anyone help me to accomplish this in DataGridView.

I'm talking about the selected part with red rectangle:

Red rectangle

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Mr. Grand
  • 33
  • 1
  • 6
  • Maybe [this](https://stackoverflow.com/questions/52484960/hide-specified-cell-borders-in-datagridview/52487741?r=SearchResults&s=1|81.8816#52487741) can help.. – TaW Dec 16 '18 at 13:46

1 Answers1

2

The borders which we can see in the specified column in the question are not cell borders. Cell borders are those dot lines between rows. So setting AdvancedBorderStyle in CellPaint method will not be much of help.

You need to perform some settings and do a bit custom painting.

These are some settings which helps you to achieve such style for rows and cells:

  • Setting Padding for the column
  • Setting row height
  • Setting cell border styles to none
  • Removing row headers
  • Handling CellPaint event:
    • Painting cell but contents.
    • Painting dotted border at top and bottom of rows.
    • Painting cell contents normally or like a text box for specific column.

Example

var specificColumn = 1;

dataGridView1.Columns[specificColumn].DefaultCellStyle.Padding = new Padding(10);
dataGridView1.RowTemplate.Height = 45;
dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;
dataGridView1.RowHeadersVisible = false;

dataGridView1.CellPainting += (obj, args) =>
{
    if (args.ColumnIndex < 0 || args.RowIndex < 0)
        return;
    args.Paint(args.CellBounds, DataGridViewPaintParts.All & 
        ~DataGridViewPaintParts.ContentForeground);
    var r = args.CellBounds;
    using (var pen = new Pen(Color.Black))
    {
        pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
        args.Graphics.DrawLine(pen, r.Left, r.Top, r.Right, r.Top);
        args.Graphics.DrawLine(pen, r.Left, r.Bottom, r.Right, r.Bottom);
    }
    r.Inflate(-8, -8);
    if (args.ColumnIndex == specificColumn)
        TextBoxRenderer.DrawTextBox(args.Graphics, r, $"{args.FormattedValue}",
            args.CellStyle.Font, System.Windows.Forms.VisualStyles.TextBoxState.Normal);
    else
        args.Paint(args.CellBounds, DataGridViewPaintParts.ContentForeground);
    args.Handled = true;
};

enter image description here

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • 1
    Right. I probably mis-read the question, focusing on *I'm talking about the selected part with red rectangle* (and the drawing). I interpreted it as a border applied to a specific Column bounds. – Jimi Dec 16 '18 at 15:29
  • 1
    @Jimi The title is talking about applying border style to specific column. In that case, `AdvancedBorderStyle` is quite useful. But the image is saying a different thing (what I interpreted in the answer). – Reza Aghaei Dec 16 '18 at 15:33
  • I think you got it right. The Cells' style shown in the image is probably not what the OP already has but what he wants to achieve. – Jimi Dec 16 '18 at 15:37