DataGridView自动调整大小,但限制最大列大小 [英] DataGridView Autosize but restrict max column size
问题描述
在我的C#4.0应用程序中,我有一个DataGridView来显示一些数据.我想要相应于内容的列大小,因此我将AutoSizeColumnsMode设置为AllCellsExceptHeader.但是我想限制列增长到超过一定值.有一个MinimumWidth属性...但是不幸的是没有MaximumWidth属性.
in my C# 4.0 Application, I have a DataGridView to display some data. I want the Columns size accordingly to the content, so I set the AutoSizeColumnsMode to AllCellsExceptHeader. But I want to restrict the columns to grow beyond a certain value. There is a MinimumWidth Property ... but unfortunately no MaximumWidth Property.
有什么办法解决这个问题吗?
Any ideas how to solve this?
预先感谢,弗兰克
推荐答案
我设法做到这一点的唯一方法是,在向其中添加行之后检查列的宽度,检查宽度,以及它的大小是否超过我的最大值.,我在将AutoSizeMode列更改为DataGridViewAutoSizeColumnMode.None
The only way i managed to do that, is to check the columns width after adding rows to it, check the width, and if it's size is above my max, i set it manually after changing the columns AutoSizeMode to DataGridViewAutoSizeColumnMode.None
foreach(DataGridViewColumn c in myView.Columns)
if (c.Width > myMax)
{
c.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
c.Width = myMax;
}
自然,添加/更新/删除行并再次执行该过程时,您需要再次将AutoSizeColumnsMode设置为AllCellsExceptHeader.
Naturally, you'd need to set the AutoSizeColumnsMode to AllCellsExceptHeader again when you add/update/delete rows and do the procedure again.
这篇关于DataGridView自动调整大小,但限制最大列大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!