Share via


DataGridViewRow.GetPreferredHeight 方法

定義

根據指定的準則,計算資料列的理想高度。

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

參數

rowIndex
Int32

已計算慣用高度的資料列索引。

autoSizeRowMode
DataGridViewAutoSizeRowMode

DataGridViewAutoSizeRowMode,指定自動調整大小模式。

fixedWidth
Boolean

true 表示要計算固定儲存格寬度的慣用高度,否則為 false

傳回

Int32

資料列的理想高度,單位為像素。

例外狀況

autoSizeRowMode 不是有效的 DataGridViewAutoSizeRowMode 值。

rowIndex 不在 0 到控制項中資料列數減 1 的有效範圍內。

範例

下列程式碼範例會 GetPreferredHeight 使用 方法來判斷已調整大小之資料列的新填補。 此程式碼範例是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 =
        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 Forms DataGridView 控制項中的調整大小選項

適用於

另請參閱