DataGridViewRow.GetPreferredHeight(Int32, DataGridViewAutoSizeRowMode, Boolean) 方法


根据指定的条件计算指定行的理想高度。Calculates the ideal height of the specified row based on the specified criteria.

 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 -> int
Public Overridable Function GetPreferredHeight (rowIndex As Integer, autoSizeRowMode As DataGridViewAutoSizeRowMode, fixedWidth As Boolean) As Integer



需要计算其首选高度的行的索引。The index of the row whose preferred height is calculated.


一个指定自动调整大小模式的 DataGridViewAutoSizeRowModeA DataGridViewAutoSizeRowMode that specifies an automatic sizing mode.


若要计算固定单元格宽度的首选高度,则为 true;否则为 falsetrue to calculate the preferred height for a fixed cell width; otherwise, false.


行的理想高度(以像素为单位)。The ideal height of the row, in pixels.


autoSizeRowMode 不是有效的 DataGridViewAutoSizeRowMode 值。autoSizeRowMode is not a valid DataGridViewAutoSizeRowMode value.

rowIndex 不在有效范围内(0 到控件中的行数减一)。The rowIndex is not in the valid range of 0 to the number of rows in the control minus 1.


下面的代码示例使用 GetPreferredHeight 方法来确定已调整大小的行的新填充。The following code example uses the GetPreferredHeight method to determine the new padding for a row that has been resized. 此代码示例摘自 How 中提供的一个更大的示例:自定义Windows 窗体 DataGridView 控件中的行的外观。This code example is part of a larger example provided in How to: Customize the Appearance of Rows in the Windows Forms DataGridView Control.

// 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 =
        DataGridViewAutoSizeRowMode.AllCellsExceptHeader, true) -

    // 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) - _

    ' 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 控件的基于内容的自动调整大小功能将使用此属性来确定行的理想高度。This property is used by the content-based automatic sizing feature of the DataGridView control to determine the ideal height of a row. 使用 rowIndex 值可以指定共享行的实际行索引。The rowIndex value lets you specify the actual row index of a shared row. (共享行的属性值 Index 为-1。)(Shared rows have Index property values of -1.)

falsefixedWidth 参数值基于计算列宽度计算行高,这将实现理想的单元格高度与宽度的比率。A fixedWidth parameter value of false calculates the row height based on calculated column widths that will achieve ideal cell height-to-width ratios.

若要将单元格内容换行到多行,对单元格有效的单元格样式的 WrapMode 属性值必须为 TrueFor cell contents to wrap onto multiple lines, the cell style in effect for the cell must have a WrapMode property value of True.

有关自动调整大小的详细信息,请参阅Windows 窗体 DataGridView 控件中的调整大小选项For more information about automatic sizing, see Sizing Options in the Windows Forms DataGridView Control.