DataGridView row height

Hi,

I need to calculate the height of a datagridview row before it is created, but I can't figure out how to do it. I am trying to fill a datagridview with only as many rows as will fit. In order to do that I need to know how tall the rows are going to be.

I guess I could create the first row and then measure it, but I figured there had to be a more elegant way to do it.

Thanks in advance.

Lee

[433 byte] By [lgrainger] at [2008-1-4]
# 1
Datagridview.RowTemplate.Height
KenTucker at 2007-9-26 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2

I tried that and it didn't seem to work. Using the default font, font size and other settings, I know that the row height should be 20 but the RowTemplate.Height shows 22. When I change the font size to a much larger font, the RowTemplate.Height stays at 22.

Am I missing a step when I initialize the DataGridView? Is there something I need to do to force the DataGridView to update the RowTemplate to reflect the current font and other settings? I have tried modifying DataGridView.Font, DataGridView.DefaultCellStyle.Font and DataGridView.RowTemplate.DefaultCellStyle.Font.

Lee

lgrainger at 2007-9-26 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3
I'm using this code in a scroll event handler to adjust the height of rows that should be visible:

int h = DataGridViewCell.MeasureTextHeight(graphics, GetCellValue(sender, i, rowNumber),mainDataGridView.DefaultCellStyle.Font, Convert.ToInt32((columnsIdea as Column).width), MakeFromCellStyle(mainDataGridView.DefaultCellStyle)) + 5;

You'll note there's a fudge factor (5) added as the text height includes neither vertical margins nor the cell border.

Hope this helps

Mike

PS: here's the code to generate the TextFormatFlags needed for MeasureTextHeight

// generate TextFormatFlags from a DataGridViewCellStyle

// found at http://mnow.wankuma.com/cs2005_datagridview_control7.html (text in Japanese except for code)

private TextFormatFlags MakeFromCellStyle(DataGridViewCellStyle style)

{

TextFormatFlags flags = new TextFormatFlags();

switch (style.Alignment)

{

case DataGridViewContentAlignment.TopLeft: flags |= TextFormatFlags.Top | TextFormatFlags.Left; break;

case DataGridViewContentAlignment.TopCenter: flags |= TextFormatFlags.Top | TextFormatFlags.HorizontalCenter; break;

case DataGridViewContentAlignment.TopRight: flags |= TextFormatFlags.Top | TextFormatFlags.Right; break;

case DataGridViewContentAlignment.MiddleLeft: flags |= TextFormatFlags.HorizontalCenter | TextFormatFlags.Left; break;

case DataGridViewContentAlignment.MiddleCenter: flags |= TextFormatFlags.HorizontalCenter | TextFormatFlags.HorizontalCenter; break;

case DataGridViewContentAlignment.MiddleRight: flags |= TextFormatFlags.HorizontalCenter | TextFormatFlags.Right; break;

case DataGridViewContentAlignment.BottomLeft: flags |= TextFormatFlags.Bottom | TextFormatFlags.Left; break;

case DataGridViewContentAlignment.BottomCenter: flags |= TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter; break;

case DataGridViewContentAlignment.BottomRight: flags |= TextFormatFlags.Bottom | TextFormatFlags.Right; break;

}

if (style.WrapMode == DataGridViewTriState.True) flags |= TextFormatFlags.WordBreak;

return flags;

}

mikebk at 2007-9-26 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4

set the minimumRow height

dgvImport.RowTemplate.MinimumHeight = 23

v_m at 2007-9-26 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5

Actually, I don't want to set the row height. I want to determine what it is. For example, if the user selects a really large font then a minimum row height of 23 is not going to work. I want the datagridview to continue calculating the appropriate row height based on the font size and other settings. I just want to know what that calculated value it.

Lee

lgrainger at 2007-9-26 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...