DataGridViewRow.GetPreferredHeight Metoda

Definicja

Oblicza idealną wysokość określonego wiersza na podstawie określonych kryteriów.

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

Parametry

rowIndex
Int32

Indeks wiersza, którego preferowana wysokość jest obliczana.

autoSizeRowMode
DataGridViewAutoSizeRowMode

Element DataGridViewAutoSizeRowMode określający tryb automatycznego określania rozmiaru.

fixedWidth
Boolean

true aby obliczyć preferowaną wysokość stałej szerokości komórki; w przeciwnym razie , false.

Zwraca

Int32

Idealna wysokość wiersza w pikselach.

Wyjątki

autoSizeRowMode jest nieprawidłową DataGridViewAutoSizeRowMode wartością.

Wartość rowIndex nie znajduje się w prawidłowym zakresie od 0 do liczby wierszy w kontrolce minus 1.

Przykłady

Poniższy przykład kodu używa GetPreferredHeight metody do określenia nowego wypełnienia dla wiersza, którego rozmiar został zmieniony. Ten przykład kodu jest częścią większego przykładu przedstawionego w temacie How to: Customize the Appearance of Rows in the Windows Forms DataGridView Control (Dostosowywanie wyglądu wierszy w kontrolce 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

Uwagi

Ta właściwość jest używana przez funkcję automatycznego określania rozmiaru DataGridView kontrolki opartej na zawartości w celu określenia idealnej wysokości wiersza. Wartość rowIndex umożliwia określenie rzeczywistego indeksu wierszy udostępnionego wiersza. (Udostępnione wiersze mają Index wartości właściwości -1).

fixedWidth Wartość parametru oblicza false wysokość wiersza na podstawie szerokości kolumn obliczeniowych, które osiągną idealne współczynniki wysokości do szerokości komórki.

Aby zawartość komórki zawijała się na wiele wierszy, styl komórki w efekcie dla komórki musi mieć WrapMode wartość Truewłaściwości .

Aby uzyskać więcej informacji na temat automatycznego określania rozmiaru, zobacz Opcje określania rozmiaru w kontrolce dataGridView Windows Forms.

Dotyczy

Zobacz też