DataGridViewRow.GetPreferredHeight 方法      
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
根据指定的条件计算指定行的理想高度。
public:
 virtual int GetPreferredHeight(int rowIndex, System::Windows::Forms::DataGridViewAutoSizeRowMode autoSizeRowMode, bool fixedWidth);public virtual int GetPreferredHeight(int rowIndex, System.Windows.Forms.DataGridViewAutoSizeRowMode autoSizeRowMode, bool fixedWidth);abstract member GetPreferredHeight : int * System.Windows.Forms.DataGridViewAutoSizeRowMode * bool -> int
override this.GetPreferredHeight : int * System.Windows.Forms.DataGridViewAutoSizeRowMode * bool -> intPublic Overridable Function GetPreferredHeight (rowIndex As Integer, autoSizeRowMode As DataGridViewAutoSizeRowMode, fixedWidth As Boolean) As Integer参数
- rowIndex
- Int32
需要计算其首选高度的行的索引。
- autoSizeRowMode
- DataGridViewAutoSizeRowMode
一个指定自动调整大小模式的 DataGridViewAutoSizeRowMode。
- fixedWidth
- Boolean
若要计算固定单元格宽度的首选高度,则为 true;否则为 false。
返回
行的理想高度(以像素为单位)。
例外
              autoSizeRowMode 不是有效的 DataGridViewAutoSizeRowMode 值。
              rowIndex 不在有效范围内(0 到控件中的行数减一)。
示例
下面的代码示例使用 GetPreferredHeight 方法确定已调整大小的行的新填充。 此代码示例是How to: Customize the Appearance of Rows in the Windows 窗体 DataGridView 控件中提供的更大示例的一部分。
// Adjusts the padding when the user changes the row height so that 
// the normal cell content is fully displayed and any extra
// height is used for the content that spans multiple columns.
void dataGridView1_RowHeightChanged(object sender,
    DataGridViewRowEventArgs e)
{
    // Calculate the new height of the normal cell content.
    Int32 preferredNormalContentHeight =
        e.Row.GetPreferredHeight(e.Row.Index, 
        DataGridViewAutoSizeRowMode.AllCellsExceptHeader, true) -
        e.Row.DefaultCellStyle.Padding.Bottom;
    // Specify a new padding.
    Padding newPadding = e.Row.DefaultCellStyle.Padding;
    newPadding.Bottom = e.Row.Height - preferredNormalContentHeight;
    e.Row.DefaultCellStyle.Padding = newPadding;
}
' Adjusts the padding when the user changes the row height so that 
' the normal cell content is fully displayed and any extra
' height is used for the content that spans multiple columns.
Sub dataGridView1_RowHeightChanged(ByVal sender As Object, _
    ByVal e As DataGridViewRowEventArgs) _
    Handles dataGridView1.RowHeightChanged
    ' Calculate the new height of the normal cell content.
    Dim preferredNormalContentHeight As Int32 = _
        e.Row.GetPreferredHeight(e.Row.Index, _
        DataGridViewAutoSizeRowMode.AllCellsExceptHeader, True) - _
        e.Row.DefaultCellStyle.Padding.Bottom()
    ' Specify a new padding.
    Dim newPadding As Padding = e.Row.DefaultCellStyle.Padding
    newPadding.Bottom = e.Row.Height - preferredNormalContentHeight
    e.Row.DefaultCellStyle.Padding = newPadding
End Sub
注解
该控件的基于内容的自动调整大小功能 DataGridView 使用此属性来确定行的理想高度。 使用 rowIndex 值可以指定共享行的实际行索引。  (共享行的属性值为 Index -1.) 
              fixedWidth参数值 false 根据计算列宽计算行高,这些列宽可实现理想的单元格高度与宽度之比。
要使单元格内容换行到多行上,对单元格生效的单元格样式必须具有 WrapMode 属性值 True。
有关自动调整大小的详细信息,请参阅 Windows 窗体 DataGridView 控件中的调整大小选项。