当前位置:  开发笔记 > 编程语言 > 正文

InvalidOperationException:在调整自动填充列时无法执行此操作

如何解决《InvalidOperationException:在调整自动填充列时无法执行此操作》经验,为你挑选了1个好方法。

我有一个形式DataGridView,我想集中的列AutoSizeModeFill与栅格ColumnHeadersHeightSizeModeAutoSize.我的问题是,如果鼠标光标在表单加载时意外地悬停在网格的左上角单元格,则应用程序会抛出一个InvalidOperationException.

这是表单加载时我应该看到的: 在此输入图像描述 (注意光标是如何悬停在左上角的单元格上).

此代码将引发异常:

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资源管理器或命令提示符运行该应用程序以查看错误.

这是堆栈跟踪:

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)

两个问题针对同一个问题:此处和此处,但在应用建议的答案时应用程序仍然崩溃.

我是否在提供的示例中打破了某种最佳做法?有没有人遇到过这种行为并知道解决方法?



1> Ivan Stoev..:

这似乎是一个错误 - 代码试图访问dataGridView.TopLeftHeaderCell,这在第一次发生时实际上创建了该单元格并触发了当时不期望的一些布局操作.

考虑到所有这些,修复很简单.我们需要确保TopLeftHeaderCellDataGridView处理之前创建它,方法是添加以下行(例如在添加网格之前Controls)

var topLeftHeaderCell = grid.TopLeftHeaderCell; // Make sure TopLeftHeaderCell is created

推荐阅读
李桂平2402851397
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有