Databinding failed, property not found?

4 posts, 0 answers
  1. Diego
    Diego avatar
    2 posts
    Member since:
    Oct 2020

    Posted 22 Oct Link to this post

    Hi. I'm new to the RadGrid, and I'm having an error when trying to databind it to a collection of objects coming from an externally provided component.

    When I try to databind it, I get a message saying "[TypeName] does not contain a property with the name [PropertyName]".

    While the object does have a member with the specified name, I couldn't be sure whether said member is a property or a regular field.

    Does this make a difference for RadGrid?

    If so, is there any workaround besides having to pester my component's provider to change the way their data objects are defined?

    Thanks in advance.

  2. Doncho
    Admin
    Doncho avatar
    72 posts

    Posted 26 Oct Link to this post

    Hi Diego,

    Could you please ensure if the values set to the DataKeyNames or/and ClientDataKeyNames properties of the RadGrid MasterTableView (or of any hierarchically nested GridTableViews) are existing accessible(public) properties of the Object class?

    Here is a basic sample:

    Declaration:

    <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" Width="800px" OnNeedDataSource="RadGrid1_NeedDataSource">
        <MasterTableView AutoGenerateColumns="False" DataKeyNames="OrderID" >
            <Columns>
                <telerik:GridBoundColumn DataField="OrderID" DataType="System.Int32"
                    FilterControlAltText="Filter OrderID column" HeaderText="OrderID"
                    ReadOnly="True" SortExpression="OrderID" UniqueName="OrderID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="ShipName"
                    FilterControlAltText="Filter ShipName column" HeaderText="ShipName"
                    SortExpression="ShipName" UniqueName="ShipName">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="ShipCountry"
                    FilterControlAltText="Filter ShipCountry column" HeaderText="ShipCountry"
                    SortExpression="ShipCountry" UniqueName="ShipCountry">
                </telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    C# code:

    protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        (sender as RadGrid).DataSource = GetOrders();
    }
    
    private List<Order> GetOrders()
    {
        List<Order> orders = new List<Order>();
        for (int i = 0; i < 10; i++)
        {
            var index = i + 1;
            var order = new Order()
            {
                OrderId = index,
                ShipName = "ShipName " + index,
                ShipCountry = "Country " + index
            };
            orders.Add(order);
        }
        return orders;
    }
    
    public class Order
    {
        public int OrderId { get; set; }
        public string ShipName { get; set; }
        public string ShipCountry { get; set; }
    }
    You may find it useful to check out the following resources:

    Kind regards,
    Doncho
    Progress Telerik

    Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

  3. Diego
    Diego avatar
    2 posts
    Member since:
    Oct 2020

    Posted 26 Oct in reply to Doncho Link to this post

    Well, I probably didn't make the question clear enough.

    The thing is, your hypothetical Order object would be defined as 

     

    public class Order {
        public int OrderId;
        public string ShipName;
        public string ShipCountry;
    }
  4. Doncho
    Admin
    Doncho avatar
    72 posts

    Posted 29 Oct Link to this post

    Hi Diego,

    Thank you for the clarification!

    I am afraid that in general, data binding to objects in the .Net framework itself, is only allowed with properties but not with fields, Databinding Web Forms to Objects as opposed to Datasets

    Therefore, some additional implementation would be needed in advance, to prepare the data in a bindable format. Find more information about Binding RadGrid in the Telerik RadGrid Data Binding section in our documentation.

    Kind regards,
    Doncho
    Progress Telerik

    Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Back to Top