UpdateRow of DataGrid inline edit causes KeyNotFoundException #802

Closed
opened 2026-01-29 17:44:37 +00:00 by claunia · 10 comments
Owner

Originally created by @Wolfware2023 on GitHub (Apr 6, 2023).

I am replicating the example DataGrid inline edit
Displaying and switching to edit mode works fine. But when I change a text value and save it, I get an error during the call of
await objectGrid.UpdateRow(currentObject);

The error message is

Error: System.Collections.Generic.KeyNotFoundException: The given key 'MyObject { RowGuid = bb19826e-70fd-5a5c-9eeb-0567cd3ac95d, TextProperty = NewText,  CreatedAt = 06.04.2023 13:13:40, CreatedById = ab460de4-5431-479f-b064-d303a58a2023, EditedAt = , EditedById =  }' was not present in the dictionary.
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at Radzen.Blazor.RadzenDataGrid`1.UpdateRow(TItem item)
   at Pages.ExamplePage.SaveRow(MyObject currentObject) in 

To Reproduce
Steps to reproduce the behavior:

  1. Copy the code from the example
  2. Remove all code with db context
  3. Replace order with your own object. Mine only had 4 string properties.
  4. See error

Expected behavior
It should work without error like in the example, calling the method defined for RowUpdate next.

  • OS: Windows
  • Browser Edge (111.0.1661.54)
  • Version 4.8.4

Additional context

  • I tried adding the Id property of type Guid, but still got the error.
  • I added KeyProperty to RadzenDateGrid to let it know about my id property, but the error prevails.
  • I also added the Attribute [Key] to the key, same error.
  • I tried a little older version of radzen, also did not change anything.
Originally created by @Wolfware2023 on GitHub (Apr 6, 2023). I am replicating the example [DataGrid inline edit](https://blazor.radzen.com/datagrid-inline-edit) Displaying and switching to edit mode works fine. But when I change a text value and save it, I get an error during the call of `await objectGrid.UpdateRow(currentObject);` The error message is ``` Error: System.Collections.Generic.KeyNotFoundException: The given key 'MyObject { RowGuid = bb19826e-70fd-5a5c-9eeb-0567cd3ac95d, TextProperty = NewText, CreatedAt = 06.04.2023 13:13:40, CreatedById = ab460de4-5431-479f-b064-d303a58a2023, EditedAt = , EditedById = }' was not present in the dictionary. at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at Radzen.Blazor.RadzenDataGrid`1.UpdateRow(TItem item) at Pages.ExamplePage.SaveRow(MyObject currentObject) in ``` **To Reproduce** Steps to reproduce the behavior: 1. Copy the code from the example 2. Remove all code with db context 3. Replace order with your own object. Mine only had 4 string properties. 4. See error **Expected behavior** It should work without error like in the example, calling the method defined for RowUpdate next. - OS: Windows - Browser Edge (111.0.1661.54) - Version 4.8.4 **Additional context** - I tried adding the Id property of type Guid, but still got the error. - I added KeyProperty to RadzenDateGrid to let it know about my id property, but the error prevails. - I also added the Attribute [Key] to the key, same error. - I tried a little older version of radzen, also did not change anything.
Author
Owner

@enchev commented on GitHub (Apr 6, 2023):

Hey @Wolfware2023,

Please post runnable code replicating the exception.

@enchev commented on GitHub (Apr 6, 2023): Hey @Wolfware2023, Please post runnable code replicating the exception.
Author
Owner

@Wolfware2023 commented on GitHub (Apr 6, 2023):

While removing more company code, I noticed my mistake. MyObject was not a class, but of type record. That caused the exception.
Now with it being a class, it works as expected. Thanks.

@Wolfware2023 commented on GitHub (Apr 6, 2023): While removing more company code, I noticed my mistake. MyObject was not a class, but of type record. That caused the exception. Now with it being a class, it works as expected. Thanks.
Author
Owner

@XorZy commented on GitHub (Sep 9, 2023):

I have encountered the same issue.
This happens with records or any type that override Equals.
The exception happens because of this line:
cae80096e1/Radzen.Blazor/RadzenDataGrid.razor.cs (L2380C28-L2380C28)
If you use a record (which compares by value), or implement a custom comparison logic, it is more than likely the comparison will fail since you are, after all, editing the columns.
This does not happen on vanilla classes since the default comparator only compares references, not actual values.
Imho this is a bug since I don't think this should happen, especially if you specify a custom KeyProperty.

@XorZy commented on GitHub (Sep 9, 2023): I have encountered the same issue. This happens with records or any type that override Equals. The exception happens because of this line: https://github.com/radzenhq/radzen-blazor/blob/cae80096e12802de1498857272f0bce5f3b3bd77/Radzen.Blazor/RadzenDataGrid.razor.cs#L2380C28-L2380C28 If you use a record (which compares by value), or implement a custom comparison logic, it is more than likely the comparison will fail since you are, after all, editing the columns. This does not happen on vanilla classes since the default comparator only compares references, not actual values. Imho this is a bug since I don't think this should happen, especially if you specify a custom KeyProperty.
Author
Owner

@ghhv commented on GitHub (Sep 30, 2023):

Arghh.. just got bitten by this again since I'm using records for my ViewModels.. which I thought was good practice.. and doesn't seem like there's any other way to resolve this... :-(

@ghhv commented on GitHub (Sep 30, 2023): Arghh.. just got bitten by this again since I'm using records for my ViewModels.. which I thought was good practice.. and doesn't seem like there's any other way to resolve this... :-(
Author
Owner

@enchev commented on GitHub (Oct 2, 2023):

Records are immutable types, changing the data requires creating of new objects.

@enchev commented on GitHub (Oct 2, 2023): Records are immutable types, changing the data requires creating of new objects.
Author
Owner

@XorZy commented on GitHub (Oct 21, 2023):

C# records are not necessarily immutable.
One could very well want to use the automatic equality comparison of records without requiring it to be immutable, and this is in fact a supported use case.
And in any case, as I noted, regular classes which override Equals will also trigger this exception.
In my opinion it does not make sense to rely on the instance-provided equality comparison in an 'edit' context since the object is most likely going to be mutated. If it compares by anything other than reference, you are going to get bitten by this.

The editContext dictionary should override the provided equality comparison and use a reference comparer instead.
If that is not feasible, one should be able to provide a custom equality comparer to override the default, or at the very least specify which property should be used for equality comparison.

@XorZy commented on GitHub (Oct 21, 2023): C# records are not necessarily immutable. One could very well want to use the automatic equality comparison of records without requiring it to be immutable, and this is in fact a supported use case. And in any case, as I noted, regular classes which override Equals will also trigger this exception. In my opinion it does not make sense to rely on the instance-provided equality comparison in an 'edit' context since the object is most likely going to be mutated. If it compares by anything other than reference, you are going to get bitten by this. The editContext dictionary should override the provided equality comparison and use a reference comparer instead. If that is not feasible, one should be able to provide a custom equality comparer to override the default, or at the very least specify which property should be used for equality comparison.
Author
Owner

@bdongus commented on GitHub (Apr 20, 2024):

I ran into this issue too. Supporting mutable records would be really nice.

@bdongus commented on GitHub (Apr 20, 2024): I ran into this issue too. Supporting mutable records would be really nice.
Author
Owner

@Wolfware2023 commented on GitHub (Sep 3, 2024):

Ok sure. I'll reopen this issue then.

@Wolfware2023 commented on GitHub (Sep 3, 2024): Ok sure. I'll reopen this issue then.
Author
Owner

@enchev commented on GitHub (Sep 3, 2024):

Hey @Wolfware2023,
We accept pull requests also!

@enchev commented on GitHub (Sep 3, 2024): Hey @Wolfware2023, We accept pull requests also!
Author
Owner

@akorchev commented on GitHub (Mar 30, 2025):

Closing as we don't plan to work on that. Would consider a pull request that adds support for this.

@akorchev commented on GitHub (Mar 30, 2025): Closing as we don't plan to work on that. Would consider a pull request that adds support for this.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/radzen-blazor#802