Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Underlining datagridview column caption

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andrus - 02 Mar 2008 22:48 GMT
.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.
Fredo - 02 Mar 2008 22:59 GMT
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.

> .NET 2 Winforms DataGridView.
>
[quoted text clipped - 29 lines]
>
> Andrus.
Fredo - 02 Mar 2008 23:01 GMT
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.
Andrus - 03 Mar 2008 18:19 GMT
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)
...

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.