且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

DataGridView自动调整大小,但限制最大列大小

更新时间:2023-01-13 21:39:23

我设法做到这一点的唯一方法是,在向其中添加行之后检查列的宽度,检查宽度,以及它的大小是否超过我的最大值.,我在将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.