AutoGenerateColumns=false causes NullReferenceException

I am new to this control, so I must be doing something wrong. I need to specify my columns manually, and to add/remove columns when the device orientation changes. I have been able to do this in my Xamarin.iOS project (although I encountered exceptions initially on second run of my method, which I was able to fix), but I encounter a NullReferenceException when I attempt to do this in my Xamarin.Android app. Unfortunately there's no stack trace, so I don't even know here to start. Again, this same code works in my Xamarin.iOS app. 

To accomplish this, I am getting a reference to the data grid OnCreateView in my fragment, then using it OnStart and OnResume in my method that specifies which columns to display. This causes a NullReferenceException with no stack trace, so I don't know where to begin to troubleshoot. If I comment out this customization method, the data grid renders without exceptions, but it's showing all columns, which I don't want. 

My method looks something like this:

var columns = new Columns();
DataGrid.AutoGenerateColumns = false;
GridTextColumn keyColumn = new GridTextColumn();
keyColumn.MappingName = "Id";
keyColumn.HeaderText = "ID";
columns.Add(keyColumn);
DataGrid.Columns = columns;

How do I accomplish this? Is there a sample I can review to figure it out for myself? It may be worth noting that my grid is in a Fragment in a ViewPager, and that I'm using MvvmCross 5.7

1 Reply

SK Shivagurunathan Kamalakannan Syncfusion Team July 14, 2018 02:11 PM UTC

Hi Robert,  
  
We have analyzed the given code. We could able to reproduce the reported issue “NullReferenceException” in Xamarin.Android. We will consider the reported issue and the fix for the issue will be available in our upcoming release. However you can also fix it in the sample level itself as shown below. 
 
Approach 1: Assign columns collection before adding the columns 
 
  
var columns = new Columns(); 
GridTextColumn keyColumn = new GridTextColumn(); 
keyColumn.MappingName = "CustomerID"; 
keyColumn.HeaderText = "Customer ID"; 
dataGrid.Columns = columns; 
columns.Add(keyColumn); 
  
  
Approach 2: Add the created columns in the dataGrid.Columns collection directly.  
 
  
var columns = new Columns(); 
GridTextColumn keyColumn = new GridTextColumn(); 
keyColumn.MappingName = "CustomerID"; 
keyColumn.HeaderText = "Customer ID"; 
dataGrid.Columns.Add(keyColumn); 
  
  
 
Please refer the below UG link for more details regarding manually adding columns. 
  
We have prepared  a sample based on your requirement and you can it from the below link. 
  
Regards, 
Shivagurunathan 


Loader.
Up arrow icon