当 datagrid 处于填充状态时,C# Datagridview 将列宽设置为首选大小
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在父级(表单)的 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. :)