0% found this document useful (0 votes)
45 views

DataGridView - autoSizeColumnsMode Property (System - Windows.forms) Microsoft Learn

This document discusses the DataGridView.AutoSizeColumnsMode property in C#. It defines the property, provides examples of using it, and notes some key remarks about column sizing modes and adjusting column widths. The property controls how column widths are determined in the DataGridView.

Uploaded by

cps5sq5vgn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

DataGridView - autoSizeColumnsMode Property (System - Windows.forms) Microsoft Learn

This document discusses the DataGridView.AutoSizeColumnsMode property in C#. It defines the property, provides examples of using it, and notes some key remarks about column sizing modes and adjusting column widths. The property controls how column widths are determined in the DataGridView.

Uploaded by

cps5sq5vgn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Microsoft Build

21– 23/05/2024

Đăng ký ngay T

O Learn  Đăng nhập

 / DataGridView / Properties / C# S 

DataGridView.AutoSize
ColumnsMode Property
Reference

 Feedback

Trong bài viết này

Definition
Examples
Remarks
Applies to
See also

Definition
Namespace: System.Windows.Forms
Assembly: System.Windows.Forms.dll

Gets or sets a value indicating how column widths are


determined.

C# = Sao chép

public
System.Windows.Forms.DataGridViewAutoSizeColum
AutoSizeColumnsMode { get; set; }

Property Value
DataGridViewAutoSizeColumnsMode

A DataGridViewAutoSizeColumnsMode value. The


default is None.

Exceptions
InvalidEnumArgumentException
The specified value when setting this property is not a
valid DataGridViewAutoSizeColumnsMode value.

InvalidOperationException
The specified value when setting this property is
ColumnHeader, column headers are hidden, and at
least one visible column has an AutoSizeMode
property value of NotSet.

-or-

The specified value when setting this property is Fill


and at least one visible column with an
AutoSizeMode property value of NotSet is frozen.

Examples
The following code example illustrates how to use
this property in a master/detail scenario where two
DataGridView controls display data from two tables in
a parent/child relationship. In this example, the
column sizing mode for the master control is None,
and the column widths are programmatically
initialized to fit the loaded values. The details control
is set to an automatic column sizing mode so that
columns will adjust automatically whenever the
values change (for example, when the user changes
the current row in the parent table). This example is
part of a larger example available in How to: Create a
Master/Detail Form Using Two Windows Forms
DataGridView Controls.

C# = Sao chép

private void Form1_Load(object sender, System.


{
// Bind the DataGridView controls to the B
// components and load the data from the d
masterDataGridView.DataSource = masterBind
detailsDataGridView.DataSource = detailsBi
GetData();

// Resize the master DataGridView columns


masterDataGridView.AutoResizeColumns();

// Configure the details DataGridView so t


// adjust their widths when the data chang
detailsDataGridView.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.AllCel
}

Remarks
This property lets you configure the control so that
column widths are automatically adjusted either to fill
the control or to fit cell contents. Size adjustments
occur in fill mode whenever the width of the control
changes. In content-based sizing modes, size
adjustments occur whenever cell contents change or,
if WrapMode is enabled, whenever row heights
change. Some content-based sizing modes let you
limit the size adjustment to the currently displayed
rows in order to increase performance.

To change the sizing mode for an individual column,


set its AutoSizeMode property. The default value of
this property is NotSet, indicating that the column
inherits its behavior and its InheritedAutoSizeMode
property value from the control.

Columns in fill mode divide the available control


width in proportions indicated by their FillWeight
property values. The width available for fill mode is
determined by subtracting the widths of all other
columns from the width of the client area of the
control. If this width is smaller than the combined
MinimumWidth values of all fill-mode columns, the
horizontal scroll bar is displayed, all fill-mode
columns are shown with their minimum widths, and
user column-resizing is disabled. For more
information about column fill mode, see Column Fill
Mode in the Windows Forms DataGridView Control.

Only columns with a Visible property value of true


are resized automatically, and changing the visibility
of a column does not cause resizing to occur.
Additionally, when columns are set to automatically
resize, the user cannot adjust the column widths with
the mouse.

To adjust column widths programmatically, use the


AutoResizeColumn or AutoResizeColumns methods
or set the column Width property.

For more information about content-based automatic


sizing, see Sizing Options in the Windows Forms
DataGridView Control.

Applies to
Sản phẩm Phiên bản

.NET 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1,
Framework 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Windows 3.0, 3.1, 5, 6, 7, 8, 9


Desktop

See also
DataGridViewAutoSizeColumnsMode
FillWeight
DataGridView Control (Windows Forms)

6 Cộng tác với chúng tôi trên GitHub


Bạn có thể tìm thấy nguồn cho nội dung này
trên GitHub, nơi bạn cũng có thể tạo và xem
lại các vấn đề và yêu cầu kéo. Để biết thêm
thông tin, hãy xem hướng dẫn dành cho
người đóng góp của chúng tôi.

Ý kiến phản hồi về .NET


.NET là một dự án nguồn mở. Chọn
liên kết để cung cấp ý kiến phản hồi:

 Sự cố mở tài liệu

 Cung cấp phản hồi về sản phẩm

 Tiếng Việt

Lựa chọn quyền riêng tư của bạn

0 Chủ đề S

Các phiên bản trước Blog Đóng góp

Quyền riêng tư Điều khoản sử dụng

Thương hiệu © Microsoft 2024

You might also like