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.
> 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
[quoted text clipped - 34 lines]
>>
>> Andrus.
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.
Fredo - 04 Mar 2008 21:33 GMT
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,
>
[quoted text clipped - 17 lines]
>
> Andrus.
Andrus - 05 Mar 2008 20:15 GMT
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;
}
Mel - 05 Mar 2008 20:25 GMT
where you created the font just change System.Drawing.FontStyle.Regular to
System.Drawing.FontStyle.Underline
> Fredo,
>
[quoted text clipped - 23 lines]
> dataGridViewColumn.HeaderCell.Style = dgvcellStyle;
> }
Andrus - 05 Mar 2008 21:08 GMT
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)
...