Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DGV throws IndexOutOfRangeException whenever its datasource is disposed (servicing) #4637

Merged
merged 1 commit into from
Mar 10, 2021

Conversation

RussKie
Copy link
Member

@RussKie RussKie commented Mar 3, 2021

Fixes #4216

Proposed changes

  • Add Dispose event handlers to clear DataSource of DGV and BindingSource of BindingNavigator thereby update their data source actual state

Customer Impact

  • A user won't catch the exception when closing a form or disposing a BindingSource

Regression?

  • Yes and no. .NET Framework 4.8 throws an exception when closing a form. v.4.7 doesn't throw the exception in this case. But anyway every version throws the exception if to Dispose a BindingSource component

Risk

  • Minimal

Screenshots

Before

  • A DataGridView doesn't update its Rows and Columns collections after its DataSource is disposed. And the DGV throws the exception when trying to redraw because the DGV is trying to draw items from a DataSource that doesn't already exist (send some index, eg. 4, to the collection that has 0 items, and catch IndexOutOfRangeException)
    vR6xyN1oe6

After

  • DataGridView and BindingNavigator subscribed on the BindingSource.Disposed event and release their DataSource and BindingSource to update internal collections
    mtA3PmfUl4

Test methodology

  • Unit testing
  • Manual UI testing
  • CTI

Test environment(s)

  • .NET 6.0.100-preview.1.21101.5
  • Microsoft Windows [Version 10.0.19042.804]
Microsoft Reviewers: Open in CodeFlow

…taSource (dotnet#4551)

to DataGridView and BindingNavigator.

Fixes dotnet#4216

A DataGridView threw IndexOutOfRangeException when its DataSource
 is already disposed and the DataGridView try to redraw itself,
 because its Rows and Columns are not updated
 but DataSource is already released. The DataGridView try to draw rows
 that are not exist in DataConnection, it send some index (eg. 5)
 to items collection and catch the exception because
 this index is out of empty items collection range.

Initially, the issue repoduced when a user closes a form
with DataGridView and BindingNavigator, because the form disposes
BindingSource when closing and then disposes BindingNavigator,
that try to redraw DataGridView. It is due to BindingNavigator
send UiaReturnRawElementProvider message
to Windows and it redraws DGV sometimes (looks like a bug).
We tried to cancel DGV redwawing if a form is closing.

Then we found the second case: we cought this IndexOutOfRangeException
if to just dispose DataSource without form closing.

So the issue is DataGridView Rows and Columns are not updated when
DataSource disposing. This fix uses Dispose events to set null for
DataGridView.DataSource and BindingNavigator.BindingSource
thereby call refresh of internal collections of their items.

(cherry picked from commit 3f9c8e7)
@RussKie RussKie requested a review from a team as a code owner March 3, 2021 23:13
@ghost ghost assigned RussKie Mar 3, 2021
@RussKie RussKie added this to the 5.0.5 milestone Mar 3, 2021
@RussKie RussKie linked an issue Mar 3, 2021 that may be closed by this pull request
@RussKie
Copy link
Member Author

RussKie commented Mar 3, 2021

Approved for servicing over email

@RussKie RussKie added the Servicing-approved .NET Shiproom approved the PR for merge label Mar 3, 2021
@codecov
Copy link

codecov bot commented Mar 3, 2021

Codecov Report

Merging #4637 (f23cb3b) into release/5.0 (937b05a) will increase coverage by 0.05801%.
The diff coverage is 100.00000%.

@@                  Coverage Diff                  @@
##           release/5.0       #4637         +/-   ##
=====================================================
+ Coverage     68.27670%   68.33471%   +0.05800%     
=====================================================
  Files             1421        1421                 
  Lines           510713      510837        +124     
  Branches         41580       41584          +4     
=====================================================
+ Hits            348698      349079        +381     
+ Misses          155658      155396        -262     
- Partials          6357        6362          +5     
Flag Coverage Δ
Debug 68.33471% <100.00000%> (+0.05800%) ⬆️
production 37.70745% <100.00000%> (+0.09841%) ⬆️
test 97.91131% <100.00000%> (+0.00857%) ⬆️

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

@RussKie RussKie merged commit d0bab6b into dotnet:release/5.0 Mar 10, 2021
@RussKie RussKie deleted the fix_4551 branch March 10, 2021 09:22
@ghost ghost locked as resolved and limited conversation to collaborators Jan 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Servicing-approved .NET Shiproom approved the PR for merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DataGridView filter caused IndexOutOfRangeException
3 participants