DataGridView 可调整列大小,但最后一列无法调整得更大?
For c = 0 To grd.Columns.Count - 1 grd.Columns(c).Resizable = DataGridViewTriState.True Next c
这使得我的 DataGridView 中的所有列都可以调整大小,除了最后一列。为什么? 我确实得到了一个小的可调整大小的鼠标指针...但是你只能使列更小,而不能更宽。
(如果我尝试使用 VB.net IDE 将所有列设置为可调整大小,也会出现同样的问题)
我在这里做错了什么?我是否应该能够调整我想要的任何列的大小,更大或更小?
For c = 0 To grd.Columns.Count - 1 grd.Columns(c).Resizable = DataGridViewTriState.True Next c
That allows all the columns in my DataGridView to be resizable EXCEPT the last column. Why?
I DO get the little resizable-mouse-pointer... but you can only make the column SMALLER, never WIDER.
(Same problem if I try to set all the columns to Resizable, using the VB.net IDE)
What am I doing wrong here? Shouldn't I be able to resize ANY column I want, bigger or smaller?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您无法使最后列变宽,因为它已经填满了控件中的所有可用空间!
与中间的其他列不同,它们只是从右侧的列中窃取空间,而最后一列右侧没有列可供其窃取空间。由于它无处可去,该控件只是阻止您调整最后一列的大小。
You can't make the last column wider because it's already filling all of the available space in the control!
Unlike the other columns in the middle, which simply steal space from the column to their right, there is no column to the right of the last column for it to steal space from. Since it doesn't have anywhere to go, the control simply prevents you from resizing the last column.
我遇到了同样的问题 - 我无法调整 DataGridView 中最后一列的大小(可调整大小的属性设置为“True”)。我已将“填充模式”设置为“显示单元格”。当我将该设置更改为“未设置”时,我通常可以调整最后一列的大小。
I've had the same problem - I couldn't resize the last column in DataGridView (the resizable property was set to 'True'). I've had 'Fill mode' set to 'Displayed Cells'. When I've changed that setting to 'Not set', I can normally resize the last column.
您可能想要的是用户调整最后一列的左边缘的大小,而不是能够调整最后一列的右边缘的大小。
一个选项是将最后一列的 AutoSizeMode 设置为“填充”,将其他列设置为其他模式,例如“未设置”。这样做可以使当您调整倒数第二列的右边缘大小时,最后一列的左边缘将自动填充该空间。
What you probably want is for the user to resize the left edge of last column, not to be able to resize the right edge of the last column.
One option is to set the AutoSizeMode of the last column to 'Fill' and the other columns to some other mode, like 'Not Set'. Doing this would make it so that when you resize the right edge of the second-to-last column, then the left edge of the last column will automatically fill-in that space.
这是一个老问题,但从来没有一个好的解决方案/答案,可能永远不会,但我会添加下一个最好的事情。我在搜索中遇到了这个问题,但最终自己解决了它,所以我为任何其他遇到这个问题需要帮助的可怜人添加了答案。
不幸的是,DataGridView (DGV) 的方式不允许您增加最后一列的宽度,如果它与控件本身的宽度相反(如它添加水平滚动条,因为列比控件宽) 。如果总列不比控件宽,则可以加宽最后一列,直到达到控件的宽度。我是如何解决这个问题的:
Panel1.AutoScrollMinSize = New Size(Integer.Parse(grd.Columns.GetColumnsWidth(DataGridViewElementStates.Visible)) + 200, 0 )
我用了+200,但你可以做你喜欢的事情。这样做的作用是使面板最小滚动宽度比总列宽更宽,从而为您现在加宽最后一列提供额外的空间。它还具有优势,因为您不需要任何可能损坏 DGV 的间隔柱。
This is an old question but there wasn't ever a good solution/answer and probably never will be but I will add the next best thing. I came across this in my search but ended up solving it myself, so I'm adding an answer to any other poor soul that comes across this in need of help.
Unfortunately with the way the DataGridView (DGV) is it won't allow you to increase the width of the last column if it is against the width of the control itself (as in it's added horizontal scrollbars as the columns are wider than the control). If your total columns weren't wider than the control, you could widen that last column UNTIL you get to the width of the control. So how I solved it:
Panel1.AutoScrollMinSize = New Size(Integer.Parse(grd.Columns.GetColumnsWidth(DataGridViewElementStates.Visible)) + 200, 0)
I used +200 but you can do what you like. What this will do is make the panel min scroll width to be wider than your total column widths allowing extra space for you to now widen the last column. It also benefits as you don't need any spacer column potentially ruining your DGV.
grd_ColumnWidthChanged
event so when you do resize a column, the panel resizes so it's always wider. You may need to consider any other event if you hide/show columns etc.请尝试如下
Please Try as below
为什么不添加一个宽度设置为零的额外列,以便用户可以调整您真正想要显示的最后一个列的大小?我知道这有点笨拙!如果您关心,您可能可以将额外的零长度单元格的 resizing 属性 = false 来不允许调整大小。 这个方法对我来说不能正常工作。需要将最后一个字段绑定到某个东西,否则它之前的字段(这是我添加假字段之前的最后一列)将不允许调整大小。
对我来说,问题是由于我根据修复建议将最后一列的 AutoSizeMode 属性更改为 AllCells (这是一个 DataGridViewTextBoxColumn)而引起的,以制作多列文本框。但这并没有按照我需要的方式工作,因此我采用了不同的方式(通过创建动态文本框并将其放在我想要的位置,但保留了 AutoSizeMode = AllCells。 >
当我在最后一列上设置 AutroSizeMode = NotSet 时,DataGridView 允许用户立即调整最后一列的大小!
Why not add an extra column set to width of zero, so that the user can resize the last one you really want to display? I know it's kinda kludgy! If you care, you could probably the resizable property = false of the extra zero length cell to not allow resize. That method wasn't working properly for me. Required binding the last field to something, or else the field before it (which was my last column before adding the fake one in) would not allow resize.
For me the problem was caused by me changing the AutoSizeMode property of my last column to AllCells (which was a DataGridViewTextBoxColumn) at the suggestion of a fix to make a multicollumn textbox. But this didn't work the way I needed it to work, so I had did it a different way (by creating a dynamic textbox and putting it where I wanted it, but had left the AutoSizeMode = AllCells.
When I set AutroSizeMode = NotSet on my Last Column, then the DataGridView allows the user to resize this last column now!