Back to Devexpress

System.InvalidCastException: Unable to cast object of type 'DevExpress.Data.Async.Helpers.ReadonlyThreadSafeProxyForObjectFromAnotherThread'

blazor-403827-troubleshooting-grid-related-issues-cannot-create-an-edit-model-automatically.md

latest3.3 KB
Original Source

System.InvalidCastException: Unable to cast object of type 'DevExpress.Data.Async.Helpers.ReadonlyThreadSafeProxyForObjectFromAnotherThread'

  • Jul 31, 2024
  • 2 minutes to read

When you try to cast an object of one type to another, you may see the following exception in the browser console:

Error: System.InvalidCastException: Unable to cast object of type ‘DevExpress.Data.Async.Helpers.ReadonlyThreadSafeProxyForObjectFromAnotherThread’ to type ‘X’.

This error occurs if you use an instant feedback data source. If a data source’s AreSourceRowsThreadSafe option is set to false (the default value), you cannot cast a data item and use the {DataItem.FieldName} notation. Call the GetDataItemValue(Object, String) method to obtain the data item’s field value. Note that this method does not provide access to the data source object and you cannot handle events or call methods related to the object.

The following example gets the value of the selected item:

razor
<DxGrid Data="InstantFeedbackSource"
        AllowSelectRowByClick="true"
        SelectionMode="GridSelectionMode.Single"
        @bind-SelectedDataItem="@SelectedDataItem"
        KeyFieldName="ProductId"
        @ref="MyGrid">
    <Columns>
        <DxGridSelectionColumn />
        <DxGridDataColumn FieldName="ProductName" />
        <DxGridDataColumn FieldName="UnitPrice" />
    </Columns>
</DxGrid>

@* ... *@
@code {
    EntityInstantFeedbackSource InstantFeedbackSource { get; set; }
    NorthwindContext Northwind { get; set; }
    object SelectedDataItem { get; set; }
    IGrid MyGrid { get; set; }
    string SelectedProductInfo { get; set; }

    protected override void OnInitialized() {
        Northwind = NorthwindContextFactory.CreateDbContext();
        InstantFeedbackSource = new EntityInstantFeedbackSource(e => {
            e.KeyExpression = "ProductId";
            e.QueryableSource = Northwind.Products;
        });
    }

    void OnGetSelectedProductName() {
        SelectedProductInfo = (string)MyGrid.GetDataItemValue(SelectedDataItem, "ProductName");
    }
    @* ... *@
}
csharp
using System;
using System.Collections.Generic;

#nullable disable

namespace Grid.Northwind {
    public partial class Product {
        public Product() {
            OrderDetails = new HashSet<OrderDetail>();
        }

        public int ProductId { get; set; }
        public string ProductName { get; set; }
        public int? SupplierId { get; set; }
        public int? CategoryId { get; set; }
        public string QuantityPerUnit { get; set; }
        public decimal? UnitPrice { get; set; }
        public short? UnitsInStock { get; set; }
        public short? UnitsOnOrder { get; set; }
        public short? ReorderLevel { get; set; }
        public bool Discontinued { get; set; }

        public virtual Category Category { get; set; }
        public virtual Supplier Supplier { get; set; }
        public virtual ICollection<OrderDetail> OrderDetails { get; set; }
    }
}