DataGridView.AutoResizeRow Method

Definition

Adjusts the height of the specified row to fit the contents of its cells.

Overloads

AutoResizeRow(Int32)

Adjusts the height of the specified row to fit the contents of all its cells including the header cell.

AutoResizeRow(Int32, DataGridViewAutoSizeRowMode)

Adjusts the height of the specified row using the specified size mode.

AutoResizeRow(Int32, DataGridViewAutoSizeRowMode, Boolean)

Adjusts the height of the specified row using the specified size mode, optionally calculating the height with the expectation that column widths will subsequently be adjusted.

AutoResizeRow(Int32)

Source:
DataGridView.Methods.cs
Source:
DataGridView.Methods.cs
Source:
DataGridView.Methods.cs

Adjusts the height of the specified row to fit the contents of all its cells including the header cell.

C#
public void AutoResizeRow(int rowIndex);

Parameters

rowIndex
Int32

The index of the row to resize.

Exceptions

rowIndex is not in the valid range of 0 to the number of rows minus 1.

Examples

The following code example resizes the third row in a DataGridView to fit the column contents. This code example is part of a larger example provided in How to: Programmatically Resize Cells to Fit Content in the Windows Forms DataGridView Control.

C#
private void SizeThirdRow(Object sender, EventArgs e)
{
    dataGridView1.AutoResizeRow(
        2, DataGridViewAutoSizeRowMode.AllCellsExceptHeader);
}

Remarks

This method is useful if you want to control when a row resizes. The row height is adjusted only once per method call; if the contents of the row later change, the row will not automatically adjust. To resize all rows, use the AutoResizeRows method. To set the row to automatically resize whenever its contents change, use the AutoSizeRowsMode property.

For cell contents to wrap onto multiple lines when a row is resized, the cell style in effect for the cell must have a WrapMode property value of True.

For more information about programmatic resizing, see Sizing Options in the Windows Forms DataGridView Control.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

AutoResizeRow(Int32, DataGridViewAutoSizeRowMode)

Source:
DataGridView.Methods.cs
Source:
DataGridView.Methods.cs
Source:
DataGridView.Methods.cs

Adjusts the height of the specified row using the specified size mode.

C#
public void AutoResizeRow(int rowIndex, System.Windows.Forms.DataGridViewAutoSizeRowMode autoSizeRowMode);

Parameters

rowIndex
Int32

The index of the row to resize.

autoSizeRowMode
DataGridViewAutoSizeRowMode

One of the DataGridViewAutoSizeRowMode values.

Exceptions

autoSizeRowMode has the value RowHeader and RowHeadersVisible is false.

autoSizeRowMode is not a valid DataGridViewAutoSizeRowMode value.

rowIndex is not in the valid range of 0 to the number of rows minus 1.

Examples

The following code example resizes the third row in a DataGridView to fit the column contents. This code example is part of a larger example provided in How to: Programmatically Resize Cells to Fit Content in the Windows Forms DataGridView Control.

C#
private void SizeThirdRow(Object sender, EventArgs e)
{
    dataGridView1.AutoResizeRow(
        2, DataGridViewAutoSizeRowMode.AllCellsExceptHeader);
}

Remarks

This method is useful if you want to control when a row resizes. The row height is adjusted only once per method call; if the contents of the row later change, the row will not automatically adjust. To resize all rows, use the AutoResizeRows method. To set the row to automatically resize whenever its contents change, use the AutoSizeRowsMode property.

For cell contents to wrap onto multiple lines when a row is resized, the cell style in effect for the cell must have a WrapMode property value of True.

This overload lets you specify a sizing mode that calculates the new height based on values in a limited set of cells, such as those in displayed rows only. This improves performance when the control contains a large number of rows.

For more information about programmatic resizing, see Sizing Options in the Windows Forms DataGridView Control.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

AutoResizeRow(Int32, DataGridViewAutoSizeRowMode, Boolean)

Source:
DataGridView.Methods.cs
Source:
DataGridView.Methods.cs
Source:
DataGridView.Methods.cs

Adjusts the height of the specified row using the specified size mode, optionally calculating the height with the expectation that column widths will subsequently be adjusted.

C#
protected void AutoResizeRow(int rowIndex, System.Windows.Forms.DataGridViewAutoSizeRowMode autoSizeRowMode, bool fixedWidth);

Parameters

rowIndex
Int32

The index of the row to resize.

autoSizeRowMode
DataGridViewAutoSizeRowMode

One of the DataGridViewAutoSizeRowMode values.

fixedWidth
Boolean

true to calculate the new height based on the current width of the columns; false to calculate the height with the expectation that the column widths will also be adjusted.

Exceptions

autoSizeRowMode has the value RowHeader and RowHeadersVisible is false.

autoSizeRowMode is not a valid DataGridViewAutoSizeRowMode value.

rowIndex is not in the valid range of 0 to the number of rows minus 1.

Remarks

This method is useful if you want to control when a row resizes. The row height is adjusted only once per method call; if the contents of the row later change, the row will not automatically adjust. To resize all rows, use the AutoResizeRows method. To set the row to automatically resize whenever its contents change, use the AutoSizeRowsMode property.

For cell contents to wrap onto multiple lines when a row is resized, the cell style in effect for the cell must have a WrapMode property value of True.

This overload lets you specify a sizing mode that calculates the new height based on values in a limited set of cells, such as those in displayed rows only. This improves performance when the control contains a large number of rows.

This overload is protected and is designed to enable you to achieve ideal cell height-to-width ratios in a derived DataGridView class. If fixedWidth is false, the row height will be calculated with the expectation that you will call the AutoResizeColumn or AutoResizeColumns method next.

For more information about programmatic resizing, see Sizing Options in the Windows Forms DataGridView Control.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10