DataGridView: Width of column, height of row automatically adjusts

Source: Internet
Author: User

Note: The DataGridView control is appended from the. NET Framework version 2.0.

Describes the width of the DataGridView column and the height of the row, based on the way the contents of the cell or header are automatically adjusted (typically the contents are all represented).

Automatically adjust the width of columns automatically when content changes

The width of the DataGridView column is automatically adjusted and can be implemented using the Datagridview.autosizecolumnsmode property.

The following code is that the width of the column is automatically adjusted based on the header and the contents of all the cells.

[C #]automatically adjusts the width of the column according to the header and the contents of all cells Datagridview1.autosizecolumnsmode = Datagridviewautosizecolumnsmode.allcells;

The members of the Datagridviewautosizecolumnsmode enumeration are as follows (referenced from the Msdn"datagridviewautosizecolumnsmode enumeration).

Datagridviewautosizecolumnsmode member name Description
Allcells The column widths are adjusted to fit the contents of all the cells in the column, including the header cells.
Allcellsexceptheader The column width adjusts to fit in the column all cells except the header cell contents.
ColumnHeader The column widths are adjusted to fit the contents of the column header cell.
Displayedcells The column width adjusts to the contents of all cells (including header cells) that fit in the columns that are currently displayed on the screen.
Displayedcellsexceptheader The column width adjusts to the contents of all cells (not including header cells) that fit in the columns currently displayed on the screen.
Fill The column width adjusts to make all column widths fill the display area of the control exactly, requiring horizontal scrolling only to keep the column wider than the value of the Datagridviewcolumn.minimumwidth property. The relative column width is determined by the relative Datagridviewcolumn.fillweight property value.
None Column widths do not automatically adjust.
Automatic adjustment of line height

The height of the DataGridView line is automatically adjusted and can be implemented using the Datagridview1.autosizerowsmode property.

[C #]the height of the row is automatically adjusted according to the header and the contents of all cells Datagridview1.autosizerowsmode = Datagridviewautosizerowsmode.allcells;

The members of the Datagridviewautosizerowsmode enumeration are as follows (referenced from the Msdn"datagridviewautosizerowsmode enumeration).

Datagridviewautosizerowsmode member name Description
Allcells Adjusts the row height to fit the contents of all cells in the row, including the header cell.
Allcellsexceptheaders Adjusts the row height to fit the contents of all cells in the row, excluding header cells.
Allheaders Adjusts the row height to the content that fits the row header.
Displayedcells Adjusts the row height to fit the contents of all cells (including header cells) in the row currently displayed on the screen.
Displayedcellsexceptheaders Adjusts the row height to fit the contents of all cells (excluding header cells) in the row currently displayed on the screen.
Displayedheaders Adjusts the row height to fit the contents of the line header that is currently displayed on the screen.
None Line height does not adjust automatically.
Height of column header, automatic adjustment of row header width

The height of the column header is automatically adjusted, You can achieve this by setting the DataGridView Object Columnheadersheightsizemode property to Datagridviewcolumnheadersheightsizemode.autosize.

The width line of the row header is automatically adjusted by setting the DataGridView Object Rowheaderswidthsizemode property to Datagridviewrowheaderswidthsizemode.autosizetoallheaders (based on the contents of all row headers from Adjust) or autosizetodisplayedheaders (automatically adjusts based on the current displayed header content) or Autosizetofirstheader (automatically adjusts to the contents of the first row header).

[C #]//Column header height auto-adjust Datagridview1.columnheadersheightsizemode = Datagridviewcolumnheadersheightsizemode.autosize ; //Row header width Auto-adjust Datagridview1.rowheaderswidthsizemode = Datagridviewrowheaderswidthsizemode.autosizetoallheaders; About optimization

By setting the autoSizeColumnsMode or autoSizeRowsMode property, the column or row can be adjusted automatically based on the contents of the cell, but this method is often used to slow down. This is especially true when the number of rows or columns is relatively large.

At this point, you can replace the Allcells and set the Displayedcells, only the contents of the current display cells automatically adjust, so you can improve the speed of execution.

Only the columns that are specified are automatically adjusted

You can use the AutoSizeMode property of the specified column (DataGridViewColumn object) only if the specified column is automatically adjusted. The use method and the autoSizeColumnsMode property are basically the same.

The following is the code that automatically adjusts the width of the first column.

[C #]//The width of the first column is automatically adjusted datagridview1.columns[0]. AutoSizeMode = Datagridviewautosizecolumnmode.displayedcells;

Unlike the autoSizeColumnsMode property, the AutoSizeMode property can be set to NotSet. When set to NotSet, the auto-adjust setting of the column inherits the setting of the Datagridview.autosizecolumnsmode property. When the AutoSizeMode property is set to NotSet inherit autoSizeColumnsMode property, the inherited settings can be obtained using the Datagridviewcolumn.inheritedautosizemode property.

Automatically adjust the width of any automatically adjusted column

All columns within DataGridView are automatically adjusted only once, and can be implemented using the AutoResizeColumns method of the DataGridView object. The AutoResizeColumns method, like the autoSizeColumnsMode property, specifies the method of automatic adjustment based on the Datagridviewautosizecolumnsmode enumeration value. You can use the AutoResizeColumn method of the DataGridView object if you only automatically adjust the specified columns.

[C #]//datagridview1 The width of all columns automatically adjusts datagridview1.autoresizecolumns (Datagridviewautosizecolumnsmode.allcells); //datagridview1 The width of the first column is automatically adjusted datagridview1.autoresizecolumn (0, datagridviewautosizecolumnmode.allcells);

Add: You can omit parameters when specifying datagridviewautosizecolumnmode.allcells using AutoResizeColumns or AutoResizeColumn in the example above.

Automatic height adjustment of rows

As with column settings, use the AutoResizeRows method to automatically adjust only the height of the specified row.

[C #]//datagridview1 The height of all rows is automatically adjusted datagridview1.autoresizerows (Datagridviewautosizerowsmode.allcells); //datagridview1 The height of the first line is automatically adjusted Datagridview1.autoresizerow (0, datagridviewautosizerowmode.allcells);

Add: You can omit parameters when specifying datagridviewautosizerowmode.allcells using AutoResizeRows or AutoResizeRow in the example above.

Column header height or row header and width auto-adjust

The height of the column header and the automatic adjustment of the width of the header of the row can be achieved using the Datagridview.autoresizecolumnheadersheight method and the AutoResizeRowHeadersWidth method.

[C #]//The height of all column headers is automatically adjusted Datagridview1.autoresizecolumnheadersheight (); //Row header width Auto-adjust datagridview1.autoresizerowheaderswidth ( Datagridviewrowheaderswidthsizemode.autosizetoallheaders);

DataGridView: Width of column, height of row automatically adjusts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.