Scott
Scott

Reputation: 1922

Creating bottom and right cell borders in C# DataGridView

I'm looking for help in creating a custom border for my DataGridView. What I'm looking for is to create a style where the column header cells have a solid, bottom border only and the row headers have a solid, right border only.

I have managed to draw the columns headers border successfully by adapting this question. However, I am struggling to draw the row headers border.

The image below shows what I have so far. As you can see, the column headers have a solid black line as their border. The red line is what I've managed to draw for the row headers, but I can't seem to get the line to extend over all rows in the table. In other words, how do I get the red line to draw in the row headers cell for all rows?

current border example

This is the event handler I'm currently using

private void TransitionTable_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex == -1 && e.ColumnIndex > -1){
        e.Handled = true;
        using (Brush b = new SolidBrush(activeTable.DefaultCellStyle.BackColor)){
            e.Graphics.FillRectangle(b, e.CellBounds);
        }

        using (Pen p = new Pen(Brushes.Black)){
             p.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
              e.Graphics.DrawLine(p, new Point(0, e.CellBounds.Bottom - 1), new Point(e.CellBounds.Right, e.CellBounds.Bottom - 1));

            //This `if` statement is what I've managed to get working, but I can't get it to draw the line on all rows.
            //It only draws in the first row
            if (e.ColumnIndex == 0)
            {
                Pen b = new Pen(Brushes.Red);
                e.Graphics.DrawLine(b, new Point(e.CellBounds.Right - 1, 0), new Point(e.CellBounds.Right - 1, e.CellBounds.Bottom - 1));
            }
        }
        e.PaintContent(e.ClipBounds);
    }
}

Upvotes: 1

Views: 3600

Answers (1)

TaW
TaW

Reputation: 54433

Your code has two issues:

1) You start with a condition that only applies to the column header row (-1). You will need two code blocks with separate conditions for the row- and the column-headers, maybe like this:

private void TransitionTable_CellPainting(object sender, 
                                          DataGridViewCellPaintingEventArgs e)
{
    using (Brush b = new SolidBrush(TransitionTable.DefaultCellStyle.BackColor))
        e.Graphics.FillRectangle(b, e.CellBounds);

    e.PaintContent(e.ClipBounds);

    if (e.RowIndex == -1)  // column header
    {
        e.Graphics.DrawLine(Pens.Black, 0, e.CellBounds.Bottom - 1, 
                                        e.CellBounds.Right, e.CellBounds.Bottom - 1);
    }
    if (e.ColumnIndex == -1)  // row header (*)
    {
        e.Graphics.DrawLine(Pens.Red, e.CellBounds.Right - 1, 0, 
                                      e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
    }
    e.Handled = true;
}

Or you could avoid owner-drawing the regular cells by wrapping the whole block in a or-conditionlike this :

if (e.RowIndex == -1 || e.ColumnIndex = -1) 
{
     all the code above in here!
}

2) Your code and your screenshot do look as if you actually don't have any RowHeaders, though, and draw your line in the first data column. If that is what you want, ignore the last remark and simply change the condition (*) to

    if (e.ColumnIndex == 0)

enter image description here

Upvotes: 2

Related Questions