Hi,
I am using List of Dictionary items to bind the XamDataGrid ItemSource.
It is successfully binding the values and displaying the values in grid, but when I click on any header to sort the list, it is throwing an exeption: Object reference not set to instance of object exception.
Am i missing something?
I am building a Xamarin forms Cross Platform app and currently testing for android devices.
Hello Harjot,
Are you using Xamarin.Forms for Android or native Xamarin? Please provide a sample that demonstrates the behavior.
Hello Michael,
Thanks for the follow up.
I am using Xamarin.Forms.
Here is the sample code snippet:
xamDataGrid.ItemsSource = listData; // where listData is ObservableCollection<Dictionary<string, string>> xamDataGrid.Columns.Add(new TextColumn { HeaderText = "header1", PropertyPath = "[key1]"
});
xamDataGrid.Columns.Add(new TextColumn{HeaderText = "header2",PropertyPath = "[key2]"
xamDataGrid.Columns.Add(new TextColumn{HeaderText = "header3",PropertyPath = "[key3]"
xamDataGrid.SortDescriptions.Add(new ColumnSortDescription{ PropertyPath = "[key1]", SortDirection = ListSortDirection.Ascending });
Hello Harjot, Thank you for following up. Please modify my attached sample and demonstrate the behavior. Sorting was working fine for me as shown in the sample.
Let me know if you have any questions. App8.zip
Thanks for your help.
In the sample app that you provided you are using List of predefined Class object to bind the data grid.
But, in my case columns are dynamic based upon the result of REST api call( list of records). So number of rows are not defined.
Therefore, I want to bind the result using List of Dictionary object to the XamDataGrid.
I tried your approach above where you add a column manually then apply a sort and I didn't get an exception. Please modify my sample to align with your requirement that demonstrates the behavior and I will be happy to look into this for you. Thanks.
I modified your sample app as per my requirements, but I'm facing Object reference not set to an instance of object exception.
Here is the code snippet for the same.
DataGrid.Columns.Add(new TextColumn { HeaderText = "header1", PropertyPath = "[key1]"
}); ObservableCollection<Dictionary<string, string>> keyValuePairs = new ObservableCollection<Dictionary<string, string>>(); Dictionary<string, string> keyValues1 = new Dictionary<string, string>(); keyValues1.Add("key1", "abc"); keyValuePairs.Add(keyValues1); Dictionary<string, string> keyValues2 = new Dictionary<string, string>(); keyValues2.Add("key1", "xyz"); keyValuePairs.Add(keyValues2); DataGrid.ItemsSource = keyValuePairs; var description1 = new ColumnSortDescription { PropertyPath = "[key1]", SortDirection = ListSortDirection.Descending }; DataGrid.SortDescriptions.Add(description1);
Kindly suggest the required changes to make the code working.
Thanks.
I am not able to reproduce this. Please modify my sample above and reattach it here. Thanks.
Thanks for your support.
I have tried it in a new project as well as in your sample application and I'm getting the same error (NullReference exception at "DataGrid.SortDescriptions.Add(description1)" line).
Since, you are not able to reproduce the error after trying out my problem statement in your sample project and its working fine for you.
Is it possible for you to share that working project (using list of dictionaries) with me.
Please use this link to download my sample.
https://filetransfer.infragistics.com/public.php?service=files&t=feadc7756e53bed96da07bc34897eec9