SoFunction
Updated on 2025-03-07

DataGridView automatically sets column width and row height

Set the row height and column width automatically adjust

Set the automatic adjustment of column widths including header and all cells

//Set the automatic adjustment of column width including header and all cellsthis.dgv_PropDemo.AutoSizeColumnsMode = ;

Set the automatic adjustment of the row height including the header and all cells

//Set the automatic adjustment of the row height including the header and all cellsthis.dgv_PropDemo.AutoSizeRowsMode = ;

Set the first column to automatically adjust

//The first column is automatically adjustedthis.dgv_PropDemo.Columns[0].AutoSizeMode = ;

Set the DataGridView to automatically adjust all column widths

this.dgv_PropDemo.AutoResizeColumns();

Set the column width of the DataGridView first column to automatically adjust

this.dgv_PropDemo.AutoResizeColumn(1, );

Set the first row of DataGridView to automatically adjust the row height

this.dgv_PropDemo.AutoResizeRow(0, );

About performance:

When the cells specified by the AutoSizeColumnsMode and AutoSizeRowsMode properties are automatically adjusted, if the number of adjustments is too many, it may lead to performance degradation, especially when there are many rows and columns, because the underlying API will be called for redrawing every time it is adjusted. In this case, using DisplayedCells instead of AllCells to adjust only visible cells can reduce the adjustment of non-visible cells and thus improve performance.

This is all about this article about automatically setting column width and row height of DataGridView. I hope it will be helpful to everyone's learning and I hope everyone will support me more.