When binding to a TreeGridModel, I'm receiving a NullReferenceException. Object reference not set to an instance of object exception.
In my view I'm binding to a TreeGridModel..
@model XNamespace.Models.XModel
<div>@(Html.Infragistics().TreeGrid(Model.XTreeGridModel)) </div>
XTreeGridModel is basiscally a property in my XModel class to return a TreeGridModel.
In my XModel class, I'm constructing TreeGridModel
// Create m_treeGridModel = new TreeGridModel();
// Set properties m_treeGridModel.ID = "treeGridModel"; m_treeGridModel.AutoGenerateColumns = false; m_treeGridModel.AutoGenerateLayouts = false; //m_treeGridModel.PrimaryKey = ColumnIndex.StrategyInstId.ToString(); //m_treeGridModel.ForeignKey = ColumnIndex.PID.ToString(); m_treeGridModel.PrimaryKey = "ID"; m_treeGridModel.ForeignKey = "PID"; m_treeGridModel.LoadOnDemand = false; m_treeGridModel.InitialExpandDepth = 1; m_treeGridModel.Columns = new List<GridColumn>(); m_treeGridModel.Columns.Add(new GridColumn() { HeaderText = "ID", Key = "ID", DataType = "number", Width = "0%", Hidden = true }); m_treeGridModel.Columns.Add(new GridColumn() { HeaderText = "PID", Key = "PID", DataType = "number", Width = "0%", Hidden = true }); m_treeGridModel.Columns.Add(new GridColumn() { HeaderText = "Name", Key = "Name", DataType = "string", Width = "10%" }); m_treeGridModel.Columns.Add(new GridColumn() { HeaderText = "Date Modified", Key = "DateModified", DataType = "string", Width = "10%" }); m_treeGridModel.Columns.Add(new GridColumn() { HeaderText = "Type", Key = "Type", DataType = "string", Width = "10%" }); m_treeGridModel.Columns.Add(new GridColumn() { HeaderText = "Size", Key = "Size", DataType = "string", Width = "10%" });
XData q1 = new XData(); // XData is a class with ID, PID, Name, DateModified, Type and Size members q1.ID = 1; q1.PID = -1; q1.Name = "XName"; q1.DateModified = "XDM"; q1.Type = "XType"; q1.Size = "323";
m_xDataList.Add(q1);
XData q2 = new XData(); q2.ID = 2; q2.PID = -1; q2.Name = "XName"; q2.DateModified = "XDM"; q2.Type = "XType"; q2.Size = "323";
m_xDataList.Add(q2);
m_treeGridModel.DataSource = m_quoteDataList.AsQueryable();
If I comment out the binding statement, I'm able to see the empty TreeGrid. But, when I uncomment the above statement, I get a null reference exception.
Any pointers here please...
I have been at this for last few hours with no luck.
Any igTreeGrid experts ....need help please
Tracked down the issue.
There is a bug in the latest build of Infragistics.Web.Mvc.
I'll submit the project files to support to compare.
Hello C R,
Thank you for posting in our forum.
In this case since a ForeignKey is set the grid will attempt to create a hierarchical representation of the flat data that is supplied as a data source. In general this is necessary in order to support remote data operations (sorting, filtering etc.) so if you don’t need those you can set the option flatToHierarchicalData to false:
m_treeGridModel.flatToHierarchicalData = false;
Otherwise there are two additional properties that should be set in order for the treegrid to properly build its hierarchy, the properties are – ForeignKeyRootValue and ChildDataKey.
The ForeignKeyRootValue should be set to the foreign key value that specifies that the record is at the root level.
The ChildDataKey should be set to the name of a property in the model of type Queryable ( this property will then be populated to create a hierarchical structure).
So in order to resolve your issue you can set both of those options, for example:
m_treeGridModel.ForeignKeyRootValue = -1;
m_treeGridModel.ChildDataKey = "ChildData";
Where the data model contains a property called ChildData, for example:
public class XData
{
public int ID { get; set; }
public int PID { get; set; }
public string Name { get; set; }
public string DateModified { get; set; }
public string Size { get; set; }
public string Type { get; set; }
public List<XData> ChildData { get; set; }
}
I hope you’ll find this information useful. Let me know if you have any questions.
Best Regards,
Maya Kirova
Infragistics, Inc.
http://ko.infragistics.com/support
I am glad that you find this information helpful.
Please let us know if you need any further assistance with this matter.
Thank you. Appreciate the response