Underlining datagridview column caption

  • Thread starter Thread starter Andrus
  • Start date Start date
A

Andrus

..NET 2 Winforms DataGridView.

I noticed that if column width is small

column.HeaderCell.SortGlyphDirection = ListSortDirection.Ascending;

does not display sort glyph.

To fix this I desided to underline column caption when mouse
in clicked in column header.

I tried

dataGridViewColumn.HeaderCell.Style.Font.Underline = true;

and

DataGridViewCellStyle dgvcellStyle = new DataGridViewCellStyle();

dgvcellStyle.Font.Underline = false;

dataGridViewColumn.HeaderCell.Style = dgvcellStyle;

but both attempts cause compile error

Property or indexer 'System.Drawing.Font.Underline' cannot be assigned to --
it is read only

How to underline grid column caption at runtime ?
Or any other idea how to fix soft glyph visibility issue ?

Andrus.
 
You have to assign a new font object to dgvcellStyle.Font. You can't change
the style of a font after it's been created. So you'd basically want to take
the existing font, query its styles (size, style, family, etc) and create a
new font with everything the same, but adding underline.
 
One other thing. I haven't actually tried this, but it occurred to me, that
may not work either. I'm not sure if you can change a column style while the
is being displayed. You may have to actually create a new style. I don't
know. I suspect it'll work as I said, but if that doesn't, try replacing the
style.
 
Fredo,
One other thing. I haven't actually tried this, but it occurred to me,
that may not work either. I'm not sure if you can change a column style
while the is being displayed. You may have to actually create a new style.
I don't know. I suspect it'll work as I said, but if that doesn't, try
replacing the style.

Thank you. I tried

System.Drawing.Font f = new System.Drawing.Font(
dataGridViewColumn.HeaderCell.Style.Font,
System.Drawing.FontStyle.Regular );

dataGridViewColumn.HeaderCell.Style.Font = f;


But got excpetion since dataGridViewColumn.HeaderCell.Style.Font is null .
How to take existing column header font ?

Andrus.
 
You said: dataGridViewColumn.HeaderCell.Style.Font is null .
Did you mean: dataGridViewColumn.HeaderCell.Style is null .

The first one makes no sense in terms of what you were doing.

What it seems like you'll need to do is instantiate a new
DataGridViewCellStyle and set all the properties you need to for it, like
font, alignment, format, etc... and then set that into
dataGridViewColumn.HeaderCell.Style.
 
Fredo,
What it seems like you'll need to do is instantiate a new
DataGridViewCellStyle and set all the properties you need to for it, like
font, alignment, format, etc... and then set that into
dataGridViewColumn.HeaderCell.Style.

Thank you.
I tried code below but got compile time error shown in comment.
How to underline column header text so that all other header attributes are
not changed ?

Andrus.

foreach (DataGridViewColumn dataGridViewColumn in grid.Columns) {
DataGridViewCellStyle dgvcellStyle = new DataGridViewCellStyle();
System.Drawing.Font font = new
System.Drawing.Font(dataGridViewColumn.HeaderCell.Style.Font,
System.Drawing.FontStyle.Regular);

// Property or indexer 'System.Drawing.Font.Underline' cannot be assigned
to -- it is read only
font.Underline = true;
dgvcellStyle.Font = font;
dataGridViewColumn.HeaderCell.Style = dgvcellStyle;
}
 
where you created the font just change System.Drawing.FontStyle.Regular to
System.Drawing.FontStyle.Underline
 
Mel,
where you created the font just change System.Drawing.FontStyle.Regular to
System.Drawing.FontStyle.Underline

Thank you.

I tried

foreach (DataGridViewColumn dataGridViewColumn in grid.Columns) {
DataGridViewCellStyle dgvcellStyle = new DataGridViewCellStyle();
System.Drawing.Font font = new System.Drawing.Font(
dataGridViewColumn.HeaderCell.Style.Font,
System.Drawing.FontStyle.Underline );
dgvcellStyle.Font = font;
dataGridViewColumn.HeaderCell.Style = dgvcellStyle;
}

But got exception probably since debugger shows that

dataGridViewColumn.HeaderCell.Style.Font

is null.

dataGridViewColumn.HeaderCell.Style is not null.

How to fix ?

Andrus.

Exception details which occurs:

System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="System.Drawing"
StackTrace:
at System.Drawing.Font..ctor(Font prototype, FontStyle newStyle)
at myapp.BrowseForm`1.QueryData(Kontekst ko)
....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top