I have a form with a DataGridView and I want to set AutoSizeMode columns to Fill and ColumnHeadersHeightSizeMode grids to AutoSize . My problem is that if the mouse accidentally hovers over the top left cell of the grid when the form loads, the application throws an InvalidOperationException .
This is what I should see when the form loads:
(Notice how the cursor hovers over the top left cell).
This code will throw an exception:
static class Program { [STAThread] static void Main() {
In my configuration, Visual Studio swallows an exception, so I have to start the application from Windows Explorer or the command line to see the error.
This is 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 relate to the same problem: here and here , but the application still crashes when I apply the suggested answers.
Am I violating some good practices in the above example? Has anyone come across this behavior and knew a workaround?
source share