Question

I'd like the column header to show a unicode string.

I can do this:

dataGridView1.Columns[10].HeaderText = "\u2191"; // uparrow

...But it displays as a square box, not an up arrow as I intended.

How can I do what I want?

Was it helpful?

Solution

It is necessary to set the font for the control to be a unicode-aware font, a font that can properly display the unicode characters in question.

The default font for Windows Forms (on my machine anyway) is "Microsoft Sans Serif" , which apparently displays unicode characters as a square box.

A font that allows unicode on my machine is "Lucida Sans Unicode" but there are others.

I could set the font for the entire datagridview in the designer.

If setting the font for the entire datagridview is for some reason undesirable, it is possible to set the font for the header cell of any particular column. I don't believe this is possible in the VS designer, but it is possible in code. The font is attached to the Style property, so the code looks like this:

dataGridView1.Columns[10].HeaderText = "\u2191"; // uparrow
var style = new DataGridViewCellStyle();
style.Font = new System.Drawing.Font("Lucida Sans Unicode", 10F,
                                     System.Drawing.FontStyle.Regular,
                                     System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridView1.Columns[10].HeaderCell.Style = style;
dataGridView1.Columns[10].ToolTipText = "upvotes";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top