当 datagrid 处于填充状态时,C# Datagridview 将列宽设置为首选大小

发布于 2024-12-20 11:19:35 字数 474 浏览 2 评论 0原文

我有一个 datagridview,它设置为“填充”,因为这样就不会出现难看的空白点。现在我想调整 2 列的大小,以便完全显示内容。

使用这段代码,我可以更改一列的宽度:

DataGrid.Columns[1].Width = DataGrid.Columns[1].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true); 

但是当我将其添加到需要调整大小的其他单元格时,

DataGrid.Columns[5].Width = DataGrid.Columns[5].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);

它会覆盖第一个单元格,因此只有最后一个单元格会正确调整大小。

有谁知道如何解决这个问题?

I have a datagridview, that is set to "Fill" because that way you have no ugly blank spots. Now I want to resize 2 columns so the content is fully shown.

With this piece of code I can change the width of one column:

DataGrid.Columns[1].Width = DataGrid.Columns[1].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true); 

But when I add it for the other cell that needs to be resized

DataGrid.Columns[5].Width = DataGrid.Columns[5].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);

It overrides the first one, so only the last cell will be resized correctly.

Does anyone have an idea on how to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心碎的声音 2024-12-27 11:19:35

在父级(表单)的 Load 事件中执行此操作,而不是在构造函数中。

即使列的自动调整大小模式设置为填充,列的结果值在加载之前也不会设置。

让我知道我是否应该详细说明。 :)

Perform this in the parent's (Form's) Load event and not the constructor.

Even if the column's auto size mode is set to Fill, the columns' resulting values won't be set until it loads.

Let me know if I should elaborate. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文