Weirdness with HeaderFont in a DataGrid?

R

Russ Perry Jr

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(dataGridColumnStyle);
}
}

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;
}
 
R

Russ Perry Jr

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]
 

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

Top