I want to change the row height of the DataGridView according to the content, and also decide the minimum height.

less than 1 minute read

Thing you want to do

I want to change the row height of the DataGridView according to the content, and also decide the minimum height.

trouble

When dataGridView.AutoResizeRows is set to AllCells, the rows are changed according to the largest column in the column, but it cannot be determined by the minimum height. There is also a property called MinimumHeight, but on the contrary, it feels like the maximum height (the name is confusing).

Solution

Get the height of the largest row in each row cell.
In the example below, the minimum row is 50, and if the largest row is less than that, it is 50.

python


foreach(DataGridViewRow row in dataGridView.Rows)
{
    var cells = row.Cells.Cast<DataGridViewCell>();
    int maxHeight = cells.Max(n => n.PreferredSize.Height);

    row.Height = maxHeight > row.Height ? maxHeight : 50;
}

It is set to PreferredSize.Height instead of Height. If it is height, is it not the size that matches the content, but the Height that was originally decided as a line is acquired?