手动更改另一个时,自动更改datagridview列的宽度 [英] Changing width of datagridview column automatically when changing another manually
问题描述
大家好,
我有两个 DataGridView
停靠在我的表单
其中我将 AutoSizeMode
设置为填写
我想自动更改第二个 DataGridView
列宽度
与第一个列的 width
更改相同。
我试过这两种情况:
Hi all,
I have two DataGridView
docked in my Form
in which I am setting its AutoSizeMode
to Fill
I want to change automatically the second DataGridView
column Width
the same as the first one's column width
change.
I tried these two cases:
private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
dataGridView2.Columns[e.Column.Index].FillWeight = e.Column.FillWeight;
}
private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
dataGridView2.Columns[e.Column.Index].Width = e.Column.Width;
}
在这两种情况下,加载包含这两个 DataGridView的表单时会出现此错误
:
调整自动填充列时无法执行此操作。
请有人帮助我吗?
提前谢谢,
:)
In the two cases this error appear when loading the form that contains these two DataGridView
:
This operation cannot be performed while an auto-filled column is being resized.
Please can anyone helps me ?
Thank you in advance,
:)
推荐答案
错误消息告诉您失败的原因。您将AutoSizeMode设置为Fill,因此无法手动设置宽度。
您需要更改AutoSizeMode
要在手动设置大小的网格上NotSet
或无
。
The error message tells you why this fails. You have the AutoSizeMode set to Fill and cannot therefore set the width manually.
You will need to change theAutoSizeMode
to eitherNotSet
orNone
on the grid that you want to set the size manually on.
试试这段代码
try this code
foreach (DataGridViewColumn dcol in dataGridView1.Columns)
{
dcol.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}
dataGridView1.AutoResizeColumns();
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
这篇关于手动更改另一个时,自动更改datagridview列的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!