Skip to content

Conversation

LeafShi1
Copy link
Member

@LeafShi1 LeafShi1 commented Jul 25, 2025

Fixes #13728

Root Cause

Due to this commit 9cbce69 (line 4827), the exception thrown on .net10.0 is System.InvalidOperationException, and the exception thrown on .net8.0 is NullReferenceException.

The original code does not make a null judgment but adds an assertion without verifying whether GetPreviousColumn(...) returned a valid column. In cases where:

  • no previous column satisfies Visible & !Frozen,

dataGridViewColumn would be null, triggering an exception and terminating rendering.

Proposed changes

  • Use logical judgment instead of assertion in method ComputeVisibleColumns of DataGridView.Methods to avoid null reference exception

Customer Impact

  • When the leftmost column is hidden and the DataGridView is displaying only the last column, the rightmost column can also be successfully hidden.

Regression?

  • No

Risk

  • Minimal

Screenshots

Before

Setting DataGridViewColumn.Visible may throw NullReferenceException

BeforeChange.mp4

After

The rightmost visible column can also be successfully hidden.

AfterChange.mp4

Test methodology

  • Manually

Test environment(s)

  • .net 10.0.0-preview.7.25368.105
Microsoft Reviewers: Open in CodeFlow

…umns of DataGridView.Methods to avoid null reference exception
Copy link

codecov bot commented Jul 25, 2025

Codecov Report

❌ Patch coverage is 0% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.90525%. Comparing base (91ebb74) to head (2bd21b6).
⚠️ Report is 38 commits behind head on main.

Additional details and impacted files
@@                 Coverage Diff                 @@
##                main      #13739         +/-   ##
===================================================
- Coverage   76.90565%   76.90525%   -0.00040%     
===================================================
  Files           3260        3260                 
  Lines         643088      643090          +2     
  Branches       47601       47602          +1     
===================================================
- Hits          494571      494570          -1     
- Misses        144848      144853          +5     
+ Partials        3669        3667          -2     
Flag Coverage Δ
Debug 76.90525% <0.00000%> (-0.00040%) ⬇️
integration 18.98777% <0.00000%> (-0.01023%) ⬇️
production 51.57212% <0.00000%> (-0.00072%) ⬇️
test 97.42152% <ø> (ø)
unit 48.96373% <0.00000%> (+0.00869%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a potential null reference exception in the ComputeVisibleColumns method of DataGridView by replacing an assertion with proper null checking logic. The issue occurs when GetPreviousColumn returns null because no previous column satisfies the Visible & !Frozen criteria, which would cause the code to fail when trying to access properties of the null column.

  • Replaces ThrowInvalidOperationExceptionIfNull assertion with conditional null checking
  • Adds proper logic to handle cases where no valid previous column exists
  • Maintains the same functionality while preventing crashes in edge cases

@ricardobossan
Copy link
Member

All LGTM

@LeafShi1 LeafShi1 added the waiting-on-team This work item needs to be discussed with team or is waiting on team action in order to proceed label Aug 11, 2025
@KlausLoeffelmann KlausLoeffelmann changed the title Use logical judgment instead of assertion in method ComputeVisibleColumns of DataGridView.Methods to avoid null reference exception Use programatically check instead of assertion in method ComputeVisibleColumns of DataGridView.Methods to avoid null reference exception Aug 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-DataGridView waiting-on-team This work item needs to be discussed with team or is waiting on team action in order to proceed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Setting DataGridViewColumn.Visible may throw NullReferenceException
2 participants