First, set the row height and the width of the automatic adjustment
Set the automatic adjustment of column widths including header and all cells
1 // set the automatic adjustment of column widths including header and all cells 2 this. Dgv_propdemo.autosizecolumnsmode = Datagridviewautosizecolumnsmode.allcells;
Set row height auto-adjustment including header and all cells
1 // Set row height auto-adjustment including header and all cells 2 this. Dgv_propdemo.autosizerowsmode = Datagridviewautosizerowsmode.allcells;
Set the first column to auto-adjust
1 // The first column is automatically adjusted 2 this. dgv_propdemo.columns[0]. AutoSizeMode = Datagridviewautosizecolumnmode.displayedcells;
Set datagridview All column widths Auto-adjust
1 this. Dgv_propdemo.autoresizecolumns (datagridviewautosizecolumnsmode.allcells);
Set column width AutoFit for the first column of DataGridView
1 this. Dgv_propdemo.autoresizecolumn (1, datagridviewautosizecolumnmode.displayedcells);
Set DataGridView first line to automatically adjust row height
1 this. Dgv_propdemo.autoresizerow (0, datagridviewautosizerowmode.allcells);
About Performance:
When auto-adjusting with the cells specified by the autoSizeColumnsMode and autoSizeRowsMode properties, if the number of adjustments is excessive then it can result in performance degradation, especially in the case of more rows and columns. Because the underlying API is called for repainting every time the adjustment is made. In this case, using displayedcells instead of Allcells only adjusts the visible cells, which can reduce the adjustments of non-visible cells, thus improving performance.
DataGridView Five: Automatically set column width and row height