InvalidOperationException:在自动填充列正在调整大小时,无法执行此操作 [英] InvalidOperationException: This operation cannot be performed while an auto-filled column is being resized
问题描述
我有一个包含 DataGridView
的表单,我想将列 AutoSizeMode
设置为填充
和网格 ColumnHeadersHeightSizeMode
到 AutoSize
。我的问题是,如果鼠标光标在表单加载时意外悬停在网格的左上方单元格中,则应用程序将抛出一个 InvalidOperationException
。
I have a form with a DataGridView
and I want to set the columns AutoSizeMode
to Fill
and the grids ColumnHeadersHeightSizeMode
to AutoSize
. My problem is that if the mouse cursor accidentally hovers the upper left cell of the grid when the form loads, the application throws an InvalidOperationException
.
这是我在表单加载时应该看到的:
(注意如何光标悬停在左上角的单元格中)
This is what I should see when the form loads: (Note how the cursor is hovering the upper left cell).
此代码将引发异常:
static class Program
{
[STAThread]
static void Main()
{
// Make sure the mouse will hover upper left cell when the form loads:
var form = new MyForm { StartPosition = FormStartPosition.Manual };
form.SetDesktopLocation(Cursor.Position.X - 30, Cursor.Position.Y - 40);
Application.Run(form);
}
class MyForm : Form
{
public MyForm()
{
var grid = new DataGridView { Dock = DockStyle.Fill };
grid.Columns.Add("ColumnName", "HeaderText");
// The form will load if I remove one of the two next lines:
grid.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
grid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
Controls.Add(grid);
}
}
}
在我的配置Visual Studio燕子异常,所以我必须从Windows资源管理器或命令提示符运行应用程序来查看错误。
In my configuration Visual Studio swallows the exception, so I have to run the application from Windows Explorer or command prompt to see the error.
这是stacktrace:
This is the stacktrace:
System.InvalidOperationException: This operation cannot be performed while an auto-filled column is being resized.
at System.Windows.Forms.DataGridView.PerformLayoutPrivate(Boolean useRowShortcut, Boolean computeVisibleRows, Boolean invalidInAdjustFillingColumns, Boolean repositionEditingControl)
at System.Windows.Forms.DataGridView.SetColumnHeadersHeightInternal(Int32 columnHeadersHeight, Boolean invalidInAdjustFillingColumns)
at System.Windows.Forms.DataGridView.AutoResizeColumnHeadersHeight(Boolean fixedRowHeadersWidth, Boolean fixedColumnsWidth)
at System.Windows.Forms.DataGridView.OnColumnHeadersGlobalAutoSize()
at System.Windows.Forms.DataGridView.set_TopLeftHeaderCell(DataGridViewHeaderCell value)
at System.Windows.Forms.DataGridView.get_TopLeftHeaderCell()
at System.Windows.Forms.DataGridView.GetCellInternal(Int32 columnIndex, Int32 rowIndex)
at System.Windows.Forms.DataGridView.OnCellMouseEnter(DataGridViewCellEventArgs e)
at System.Windows.Forms.DataGridView.UpdateMouseEnteredCell(HitTestInfo hti, MouseEventArgs e)
at System.Windows.Forms.DataGridView.OnColumnWidthChanged(DataGridViewColumnEventArgs e)
at System.Windows.Forms.DataGridView.OnBandThicknessChanged(DataGridViewBand dataGridViewBand)
at System.Windows.Forms.DataGridViewBand.set_ThicknessInternal(Int32 value)
at System.Windows.Forms.DataGridView.AdjustFillingColumns()
at System.Windows.Forms.DataGridView.ComputeLayout()
at System.Windows.Forms.DataGridView.PerformLayoutPrivate(Boolean useRowShortcut, Boolean computeVisibleRows, Boolean invalidInAdjustFillingColumns, Boolean repositionEditingControl)
at System.Windows.Forms.DataGridView.OnHandleCreated(EventArgs e)
at System.Windows.Forms.Control.WmCreate(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
两个问题针对同一个问题:其中和这里,但是当我应用建议的答案时应用程序仍然崩溃
Two questions targets the same issue: Here and here, but the application still crashes when I apply the suggested answers.
在提供的示例中,我是否打破了一些最佳实践?
有人以前遇到过这个行为,知道解决方法吗?
Am I breaking some kind of best practice in the provided example? Has anyone come across this behavior before and know a workaround?
推荐答案
这似乎是一个错误 - 代码正在尝试访问 dataGridView.TopLeftHeaderCell
,当第一次实际创建该单元格并触发某些预期的布局操作时。
This seems to be a bug - the code is trying to access dataGridView.TopLeftHeaderCell
, which when happens for the first time actually creates that cell and triggers some layout actions not expected at that moment.
考虑到这一切,修复很简单。我们需要确保 TopLeftHeaderCell
是在 DataGridView
句柄之前创建的,通过添加以下行(在添加网格之前)到控件
)
With all that in mind, the fix is simple. We need to make sure that the TopLeftHeaderCell
is created before DataGridView
handle, by adding the following line (before addding the grid to Controls
for instance)
var topLeftHeaderCell = grid.TopLeftHeaderCell; // Make sure TopLeftHeaderCell is created
这篇关于InvalidOperationException:在自动填充列正在调整大小时,无法执行此操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!