Search code examples
.netdatagridviewrowsautoresize

Datagridview's row autoresize


I'm trying to automatically adjust a row's height and I've found it very challenging.

I've already set this property :

DataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells.

I've also made it using this other method:

DataGridView.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders)

And also row by row, by using this:

DataGridView.AutoResizeRow(i, DataGridViewAutoSizeRowMode.AllCells)

And also even hardcoded the row's height to a large value, and it didn't work either!! All the rows are shown with their defaults heights.

None of these worked. I'm running out of options.

Most of the rows in the datagridview don't need to be resized. But one of them is filled with values like these:

"a" + "\n" + b + "\n" + "c" + "\n" + "d" + "\n" + "e"

I mean, short values but in different lines. I have to show them in different lines, can't show them all together. But the datagridview shows only the first one and all the other ones are hidden, because the row doesn't autoresize.

Any idea about any other way to do it.


Solution

  • I've found the solution to this problem. Instead of working with the autosize properties of the grid or the rows, I should have used the wrapmode to be applied to all the grid's cells.

    DataGridView.DefaultCellStyle.WrapMode = DataGridViewTriState.True

    This is applied to all the cells and it works.