DataGridView How to adjust column width

Source: Internet
Author: User

DataGridView has a property that is autoSizeColumnMode, and he has several enumeration values:

1. Allcells adjusts the column width to fit the contents of all the cells in the column, including the header cell.

2. Allcellsexceptheader adjusts the column width to fit the contents of all the cells in the column, excluding the header cells.

3, ColumnHeader adjust the column width to fit the contents of the column header cell.

4. Displayedcells adjusts the column width to fit the contents of all cells in the column of the row displayed on the current screen, including the header cell.

5. Displayedcellsexceptheader adjusts the column width to fit the contents of all cells in the column of the row displayed on the current screen, excluding the header cell.

6, fill the column width, so that all the width of the column just fill the control's display area, only need horizontal scrolling to ensure that the column width in the Datagridviewcolumn.minimumwidth property value. The relative column width is determined by the relative Datagridviewcolumn.fillweight property value.

7, None column width will not automatically adjust.

8, the resizing behavior of the NotSet column inherits from the Datagridview.autosizecolumnsmode property.

Note: If you want to automatically set the column width. Here you must set the value of DataGridView to fill

This.dataGridView.DataSource = ds. Tables[0];

This.datagridview.columns[0]. Fillweight = 10; The relative width of the first column is 10%

THIS.DATAGRIDVIEW.COLUMNS[1]. Fillweight = 20; The second column has a relative width of 20%

THIS.DATAGRIDVIEW.COLUMNS[2]. Fillweight = 30; The third column has a relative width of 30%

The value here is relative to the current total width of the DataGridView, so the effect of the form maximization and narrowing is not the same, but the proportions are unchanged

Note: How to set headers for each column

Set the Title field (set Columnsheadersvisible to True first)

This.datagridview.columns[0]. HeaderText = "number";

THIS.DATAGRIDVIEW.COLUMNS[1]. HeaderText = "date";

THIS.DATAGRIDVIEW.COLUMNS[2]. HeaderText = "title";

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.