How to Bind JSON datasource directly to Datagrid in .net MAUI

1 Answer 874 Views
DataGrid
Heman
Top achievements
Rank 1
Heman asked on 23 Dec 2022, 10:50 AM

I have JSON datasource which I need to bind to Itemsource property of Datagridview. I cannot have class into which deserialization can be done because JSON datasource can have different attributes evertime which makes it impossible to have hard coded classes for deserialization.

Let me know if JSON datasource can be binded directly based on data and also Searching and Sorting should work in the grid.

Datagrid is for .net MAUI platform targetting Android and IOS development

1 Answer, 1 is accepted

Sort by
0
Deyan
Telerik team
answered on 26 Dec 2022, 12:19 PM

Hi Herman,

RadDataGrid works with only one specific object type passed to the control by the ItemsSource property. That means that you can visualize one type of data at a time. The DataGrid control cannot convert the json of the JObject to a type. This should be done by the Newtonsoft.Json package and after that the object could be set as a source for the DataGrid and be visualized as expected:

public partial class MainPage : ContentPage
{
	public MainPage()	{
		InitializeComponent();

        JObject o1 = JObject.Parse(@"{
		'FirstName': 'John',
		'LastName': 'Smith',
		}");

        Person p1 = o1.ToObject<Person>();

        this.dataGrid.ItemsSource = new List<Person>() { p1 };
    }

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

And the xaml:

<telerik:RadDataGrid x:Name="dataGrid" />

There isn't an option to directly pass the json to the DataGrid ItemsSource.

I hope this information answers your question.

Regards,
Deyan 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/.

Tags
DataGrid
Asked by
Heman
Top achievements
Rank 1
Answers by
Deyan
Telerik team
Share this question
or