Skip to content

Prevent bound sound path grid deletes from leaving stale DataGridView state#1202

Open
Copilot wants to merge 2 commits into
developfrom
copilot/fix-sound-sample-path-deletion
Open

Prevent bound sound path grid deletes from leaving stale DataGridView state#1202
Copilot wants to merge 2 commits into
developfrom
copilot/fix-sound-sample-path-deletion

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 19, 2026

Deleting a sound sample path in Level Settings could throw System.IndexOutOfRangeException after importing a legacy TRLE project and converting it to TEN. The failure came from the bound DataGridView delete path leaving stale current-row/selection state while the underlying list was being mutated.

  • Root cause

    The sound path list is backed by a bound collection. Deletion was removing items from the editable row collection while the grid still held a current cell and selected rows that could become invalid mid-update.

  • Change

    The shared DarkDataGridViewControls delete handler now clears grid state before removing rows from the bound collection:

    • clears CurrentCell
    • clears selection
    • removes rows using the captured indices in descending order
    • avoids touching SelectedRows again after the collection has changed
  • Effect

    Bound row deletion no longer depends on DataGridView rows that may already be detached, which prevents the CurrencyManager index mismatch seen when deleting sound sample paths.

List<int> selectedRowIndices = DataGridView.SelectedRows.Cast<DataGridViewRow>().Select(row => row.Index).ToList();
selectedRowIndices.Sort();
selectedRowIndices.Reverse();

DataGridView.CurrentCell = null;
DataGridView.ClearSelection();

var rows = DataGridView.EditableRowCollection;
foreach (var selectedRowIndex in selectedRowIndices)
    if (selectedRowIndex >= 0 && selectedRowIndex < rows.Count)
        rows.RemoveAt(selectedRowIndex);

Agent-Logs-Url: https://github.com/TombEngine/Tomb-Editor/sessions/7195f07c-af45-4b55-989f-6363f0a441dd

Co-authored-by: Nickelony <20436882+Nickelony@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix index out of range exception when deleting sound sample path Prevent bound sound path grid deletes from leaving stale DataGridView state Apr 19, 2026
Copilot AI requested a review from Nickelony April 19, 2026 21:09
@Nickelony Nickelony marked this pull request as ready for review April 19, 2026 21:28
@Nickelony Nickelony added bug Something isn't working as intended. ready for review The Pull Request is finished and ready for review. labels Apr 19, 2026
@Nickelony
Copy link
Copy Markdown
Collaborator

@copilot Would you be able to provide reproduction steps for the original issue? What do we have to do in order to get the original error before the fix was applied? Also, please double-check that the changes don't cause any regressions for other places in the code.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 20, 2026

@copilot Would you be able to provide reproduction steps for the original issue? What do we have to do in order to get the original error before the fix was applied? Also, please double-check ...

The original repro should be: open a legacy TRLE project, convert it to TEN, go to Level Settings -> Sounds, select a sound sample path row near the bottom of the list so it is also the current row, then delete it. Before 659a77c, the bound list was updated first, ListChanged repopulated the related sound UI, and the grid could still point at the old row index, which is what produced the CurrencyManager / Index N does not have a value dialog.

I also double-checked the regression risk in the shared delete helper. The only behavior change is that CurrentCell and selection are cleared before removal instead of after it; the old code already ended with no selection, so the post-delete state is the same. DeleteRowCheckIfCancel still runs before anything is cleared, and I verified a targeted DarkUI/DarkUI.csproj build succeeds locally on this runner. The only broader build blockers I hit are unrelated pre-existing WPF/XAML issues on Linux. Commit: 659a77c

@Nickelony Nickelony added this to the Version 2.0 milestone Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as intended. ready for review The Pull Request is finished and ready for review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TE: Sound sample path delete - System.IndexOutOfRangeException: Index 16 does not have a value.

2 participants