473,386 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Weirdness with HeaderFont in a DataGrid?

I'm working on my first "real" C# project, and one of the first things
I'm doing is putting some data up in a grid. I'm using a DataSet
returned form running a stored proc as my grid.DataSource, and
everything
comes up just fine.

Then I think, let's be clever and resize the grid columns to their max
widths, and a little research finds this page, which works fine:

http://odetocode.com/code/59.aspx

Then I added a little code make the last column stretch to the
righthand
side of the grid's area so there's no "null space", and that works. I
exclude a couple of columns, and that works.

Now... I also start playing with the font. I'm a little annoyed that
changing the grid.Font property (in the IDE) also changes the
CaptionFont
and HeaderFont, but I can deal with that.

But the odd thing is, if I call the SizeColumns method, the HeaderFont
is the same as the Font, regardless of the setting. I even tried to
add in a save and replace but that didn't work, and when I threw in
some MessageBox messages, I see that it (grid.HeaderFont) never
changes
from "Microsoft Sans Serif". However, the HeaderFont is "Courier New"
like the Font is set to. If I comment out the call to the SizeColumns
method, then the font is correct again.

Is there some drawing bug, or is there something I'm missing? The
modified method is below; anything stand out as wrong? I'm a little
bit perplexed...

protected void SizeColumns(DataGrid grid)
{
MessageBox.Show("ENTRY: " + grid.HeaderFont.Name);

// save special fonts, since this info seems to get overridden by
grid.Font
System.Drawing.Font headerFont = new Font(grid.HeaderFont.Name,
grid.HeaderFont.Size);

Graphics g = CreateGraphics();
DataTable dataTable = (DataTable) grid.DataSource;

DataGridTableStyle dataGridTableStyle;

if (grid.TableStyles.Count == 0)
{
dataGridTableStyle = new DataGridTableStyle();
dataGridTableStyle.MappingName = dataTable.TableName;
}
else
{
dataGridTableStyle = grid.TableStyles[dataTable.TableName];
}

dataGridTableStyle.RowHeadersVisible = grid.RowHeadersVisible;
dataGridTableStyle.GridColumnStyles.Clear();

int totalSize = 0;

MessageBox.Show("PRE ITERATION: " + grid.HeaderFont.Name);

foreach (DataColumn dataColumn in dataTable.Columns)
{
int maxSize = 0;

if ((dataColumn.ColumnName.ToString() != "Dest_TableName") &&
(dataColumn.ColumnName.ToString() != "Src_TABLE_NAME")) // don't
need to see these columns
{
SizeF size = g.MeasureString(dataColumn.ColumnName,
grid.HeaderFont);

if (size.Width > maxSize)
{
maxSize = (int) size.Width;
}

foreach (DataRow row in dataTable.Rows)
{
size = g.MeasureString(row[dataColumn.ColumnName].ToString(),
grid.Font);

if (size.Width > maxSize)
{
maxSize = (int) size.Width;
}
}

DataGridColumnStyle dataGridColumnStyle = new
DataGridTextBoxColumn();
dataGridColumnStyle.MappingName = dataColumn.ColumnName;
dataGridColumnStyle.HeaderText = dataColumn.ColumnName;
dataGridColumnStyle.Width = maxSize + 10;
totalSize += dataGridColumnStyle.Width;
dataGridTableStyle.GridColumnStyles.Add(dataGridCo lumnStyle);
}
}

MessageBox.Show("AFTER ITERATION: " + grid.HeaderFont.Name);
if (grid.Width > totalSize) // fill up ugly space on right
{
dataGridTableStyle.GridColumnStyles[dataGridTableStyle.GridColumnStyles.Count
- 1].Width +=
(grid.Width - totalSize - (grid.RowHeadersVisible ?
grid.RowHeaderWidth : 0) - 21);
// NOTE: the last value subtracted is to account for the width
of the scrollbar
}

if (grid.TableStyles.Count == 0)
{
grid.TableStyles.Add(dataGridTableStyle);
}

g.Dispose();

MessageBox.Show("AFTER DISPOSE: " + grid.HeaderFont.Name);
above?
// put back special fonts
grid.HeaderFont = headerFont;
}
Nov 16 '05 #1
1 1676
Man, I hate to follow up my own posts, but for posterity, and to
prevent people answering with something I eventually already figured
out...

The secret was to set the TableGridStyle.HeaderFont to the HeaderFont
of the grid. I thought I'd looked at the TableGridStyle for the
HeaderFont property and didn't see it, but maybe I looked at the
column style twice, or just read by it somehow. Grr!

But anyone having the same problem, there's your answer.

[NOTE: see prior post for full info]

sl******@rcn.com (Russ Perry Jr) wrote:
But the odd thing is, if I call the SizeColumns method, the HeaderFont
is the same as the Font, regardless of the setting. I even tried to
add in a save and replace but that didn't work, and when I threw in
some MessageBox messages, I see that it (grid.HeaderFont) never
changes
from "Microsoft Sans Serif". However, the HeaderFont is "Courier New"
like the Font is set to. If I comment out the call to the SizeColumns
method, then the font is correct again.

Is there some drawing bug, or is there something I'm missing?

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Corne' Cornelius | last post by:
Hi, I'm experiencing some weirdness in a program, when subtracting 2 (double)'s which should result in 0, but instead it returns -1.11022e-16. It looks to me that changing the double x_step...
1
by: (Pete Cresswell) | last post by:
TabControl on the right side of a form with two tabs: Tab #1 contains two subforms that show some dynamic query results. Tab #2 contains a ListView that gets dynamically populated as the user...
0
by: Bruce B | last post by:
Hi group, I'm experiencing some extreme weirdness over the last week with IIS related to it's Mappings and how .NET is behaving. I can't explain the behavior as it seems very random. Our...
1
by: VB Programmer | last post by:
My development machine has been working perfectly, writing ASP.NET apps, etc... I just went to open a project and it said: "Visual Studio .NET has detected that the specified web server is not...
5
by: Phil Weber | last post by:
I'm attempting to debug an ASP.NET Web application in VS.NET 2003. I'm running the app and the debugger on my local machine (Windows XP Professional). I am logged in as a local administrator. In...
5
by: David Thielen | last post by:
Hi; I am creating png files in my ASP .NET app. When I am running under Windows 2003/IIS 6, the file is not given the security permissions it should have. It does not have any permission for...
1
by: rhino | last post by:
I've got some positioning problems that I can't figure out. Can anyone help? My website was working fine in IE7 and the current releases of Firefox and Opera so I had a look at it in IE6 and...
26
by: Prisoner at War | last post by:
Hi, All: I have a JavaScript search engine that always causes MSIE 7 to do a top-of-page security "warning" (that top-of-page-bar, and not an "alert" )...but other websites' JavaScripts do not...
2
by: JYA | last post by:
Hi. I was writing an xmltv parser using python when I faced some weirdness that I couldn't explain. What I'm doing, is read an xml file, create another dom object and copy the element from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.