Language

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。

返品

行の理想的な高さ (ピクセル単位)。

例外

autoSizeRowMode が有効な DataGridViewAutoSizeRowMode 値ではありません。

rowIndexが、コントロール内の行数から 1 を引いた行数までの有効な範囲にありません。

例

次のコード例では、 GetPreferredHeight メソッドを使用して、サイズが変更された行の新しいパディングを決定します。 このコード例は、「 方法: 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 コントロールのサイズ設定オプション」を参照してください。

適用対象

こちらもご覧ください