Можем ли мы изменить шрифт какой-либо части текста заголовка столбца в datagridview - PullRequest
1 голос
/ 04 апреля 2019

Я знаю, как изменить шрифт заголовка столбца. Это просто.

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

Я хочу изменить шрифт части текста заголовка. Как и «Мартовская распродажа ($)», я хочу сделать только «($)» часть красной и жирной. Есть ли способ, которым мы можем?

Ответы [ 2 ]

1 голос
/ 04 апреля 2019

Думаю, вам придется нарисовать это самостоятельно.

Примерно так:

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;
    }
}
0 голосов
/ 04 апреля 2019

Вы не можете установить один и тот же заголовок с двумя разными шрифтами - но то, что вы можете сделать, это связать стопку из 2 меток с вашим столбцом

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...