Skip to content

Setting DataGridViewColumn.Visible may throw NullReferenceException #13728

@equin0x80

Description

@equin0x80

.NET version

.NET 8.0 (Windows), target OS version 7.0

Did it work in .NET Framework?

No

Did it work in any of the earlier releases of .NET Core or .NET 5+?

No response

Issue description

My app has a (subclassed) DataGridView that adds a right-click context menu, allowing the user to add and remove columns. So the user can right-click a column and choose "Hide This Column", and that DataGridViewColumn will be hidden by setting Visible=false.

The bug occurs if I have dragged the rightmost column to be very wide (much wider than the grid itself) so that only that rightmost column can be seen within the grid's scrollable bounds. I then right-click and choose to hide that column. The act of setting the DataGridViewColumn's Visible property then unexpectedly throws NullReferenceException within Microsoft code:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=System.Windows.Forms
  StackTrace:
   at System.Windows.Forms.DataGridView.ComputeVisibleColumns()
   at System.Windows.Forms.DataGridView.LayoutScrollBars()
   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.OnColumnStateChanged(DataGridViewColumnStateChangedEventArgs e)
   at System.Windows.Forms.DataGridView.OnDataGridViewElementStateChanged(DataGridViewElement element, Int32 index, DataGridViewElementStates elementState)
// this is where I set the Visible property
   at Macrame.Ui.Grids.GridContextMenuBuilder.mnuHideCol_Click(Object sender, EventArgs e) in [REDACTED]\Ui\Grids\GridContextMenuBuilder.cs:line 125

Steps to reproduce

See description above.

Note: I can reproduce this behaviour every time, in my large app, but I have not been able to reproduce it in a smaller test project. So please ask me if you need any further details about the state and properties at time of failure.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions