471,056 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,056 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 1607
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Corne' Cornelius | last post: by
1 post views Thread by (Pete Cresswell) | last post: by
5 posts views Thread by Phil Weber | last post: by
5 posts views Thread by David Thielen | last post: by
1 post views Thread by rhino | last post: by
26 posts views Thread by Prisoner at War | last post: by
2 posts views Thread by JYA | last post: by
reply views Thread by leo001 | last post: by

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.