C# DataGridView AutoSizeRowsMode 问题
我有一个 DataGridView,我以编程方式向其添加数据。 我将 AutoSizeRowsMode 设置为 AllCells,并将 RowsDefaultCellStyle 上的 WrapMode 设置为 True。 如果我向 DataGridView 添加多行行,它会显示良好(自动缩放行以显示多行)。 但是,如果我隐藏具有多行数据的列(以便剩下的列只有单行数据),则行的大小会调整为单行,但当我重新显示隐藏的列时,行的大小不会调整。 如果我调整表格大小,那么行就会正确。
以下代码显示了我如何切换列的可见性: notificationDataGridView.Columns[1].Visible = !notificationDataGridView.Columns[1].Visible;
谁能帮我解决这个问题吗? 我已经尝试了我能想到的所有无效和刷新的组合。
I have a DataGridView which I add data to programatically. I have the AutoSizeRowsMode set to AllCells and the WrapMode on RowsDefaultCellStyle set to True. If I add a multiline row to the DataGridView it shows up fine (autoscaling the row to show the multiple lines). However, if I hide the column that has the multiline data (so that the columns left only have single line data) the row resizes to single row but when I reshow the hidden column the rows aren't resized. If I resize the form then the rows will correct.
The following code shows how I am toggling the visibility of the column:
notificationDataGridView.Columns[1].Visible = !notificationDataGridView.Columns[1].Visible;
Can anyone help me out with how to fix this? I've tried every combination of Invalidate and Refresh I can think of.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我不知道这是否是 DataGridView 中的错误或其他什么,但这里有一个让它工作的技巧。 在调用:之后
添加这两行代码:
不要问我为什么,但这似乎有效。 如果谁有更好的解决方案,请留言!
Ok, I don't know if this is a bug in the DataGridView or whatever, but here's a hack to make it work. After the call to:
add these two lines of code:
Don't ask me why, but this seems to work. If anyone has a better solution, please post it!
我遇到了类似的问题,行高未正确调整大小。 在我的 DataGridView 中,我使用 CellFormatting 事件将自定义数据写入 DataGridViewTextBoxColumn,该列在相关列上具有 AutoSizeRowsMode=AllCells 和 DefaultCellStyle.WrapMode=DataGridViewTriState.True 。 我在该列的单元格文本中使用 \r\n 来添加额外的行。 更改数据以向其中一行添加额外的行后,它没有调整大小,因此没有显示我添加的额外行。
我尝试了上面的解决方法来尝试强制它重新绘制行高,但它不起作用。 不过,以下令人讨厌的解决方法确实对我有用:
奇怪的是,这个黑客只能通过重置宽度而不是高度来起作用。
I came across a similar problem with row heights not resizing correctly. In my DataGridView I am using the CellFormatting event to write custom data to a DataGridViewTextBoxColumn, which has AutoSizeRowsMode=AllCells and DefaultCellStyle.WrapMode=DataGridViewTriState.True on the relevant column. I use \r\n in the cell text for that column to add extra lines. Upon changing the data to add an extra line to one of the rows, it didn't resize so didn't show the extra line I added.
I tried the above workaround to try to force it to redraw the row heights, but it didn't work. The following nasty workaround did work for me though:
Strangely this hack only worked by resetting the Width, not the Height.