Can we change font of some part of a column's header text in datagridview

I know how to change column's header text font. It's easy.

 foreach (DataGridViewColumn col in dgKisiFatura.Columns)
            {
                col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            }

I want to change a part's font of the header text. Like "March Sale ($)", I wanna make only "($)" part red and bold. Is there any way we can?

Upvotes: 1

Views: 340

Answers (2)

Peter
Peter

Reputation: 197

I think you'd have to draw it yourself.

Something like this:

private void dgv_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {

    if (e.RowIndex == -1 && dgv.Columns[e.ColumnIndex].HeaderText == "March Sale ($)") {
        //draw non-content portion
        e.Paint(e.CellBounds, e.PaintParts ^ DataGridViewPaintParts.ContentForeground);

        //get size of text to write
        SizeF firstTextSize = e.Graphics.MeasureString("March Sale ", e.CellStyle.Font);
        SizeF secondTextSize = e.Graphics.MeasureString("($)",  new Font(e.CellStyle.Font, FontStyle.Bold));

        Point p = e.CellBounds.Location;
        //center text
        p.Offset((int)((e.CellBounds.Width-firstTextSize.Width-secondTextSize.Width)/2), (int)((e.CellBounds.Height-firstTextSize.Height)/2));

        e.Graphics.DrawString("March Sale ", e.CellStyle.Font, new SolidBrush(Color.Black), p);
        p.Offset((int)firstTextSize.Width,0);
        e.Graphics.DrawString("($)", new Font(e.CellStyle.Font, FontStyle.Bold), new SolidBrush(Color.Red), p);

        e.Handled = true;
    }
}

Upvotes: 1

Ido H Levi
Ido H Levi

Reputation: 166

You can not set the same header with 2 different fonts - but what you can do is have a stack of 2 labels bound to your column

Upvotes: 0

Related Questions