Let the DataGridView of the column fit the content

Source: Internet
Author: User

When using DataGridView operation data, in order to be beautiful, I hope the column width can adapt to the content.

In fact, one line of code:

Columns[i]. AutoSizeMode = Datagridviewautosizecolumnmode.allcells;

This is done, the effect is that the column width will be based on the content and the width of the table head to judge, and finally adjust to the appropriate width.

AutoSizeMode This property is not displayed in the Properties window.
If your DataGridView is dynamically bound to the data,

It can only be set in the code.
If your DataGridView column name is designed, you can right-click on DataGridView, select "Edit Column", then select the column you want to set, and select the type of AutoSizeMode in the Layout tab of its properties list. There are a few more values that the AutoSizeMode property can set:

member name Description
NotSet The resizing behavior of a column is inherited from the Datagridview.autosizecolumnsmode property.
None Column widths do not automatically adjust.
Allcells Adjusts the column width to fit the contents of all the cells in the column, including the header cell.
Allcellsexceptheader Adjusts the column width to fit the contents of all cells in the column, excluding header cells.
Displayedcells Adjusts the column width to fit the contents of all cells in the column of the row that is displayed on the current screen, including the header cell.
Displayedcellsexceptheader Adjusts the column width to fit the contents of all cells in the column of the row displayed on the current screen, excluding header cells.
ColumnHeader Adjusts the column width to fit the contents of the column header cell.
Fill

Adjust the width of the columns so that the widths of all the columns exactly fill the display area of the control, requiring only horizontal scrolling to ensure that the column width is above the Datagridviewcolumn.minimumwidth property value. The relative column width is determined by the relative Datagridviewcolumn.fillweight property value.

Let the DataGridView of the column fit the content

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.