How to maintain expand/collapsed state of rows in ASP.NET MVC TreeGrid?
This article explains how to maintain expand/collapse state of rows during page refresh in ASP.NET MVC TreeGrid.
You can maintain expand/collapse state of rows during page refresh using dataBound event.
- You can save the collapsed records to localStorage in collapsed event of Tree Grid by using the method setItem method.
- On page refresh, dataBound event will be triggered. In that event, retrieve the saved records by using the method getItem method and collapse the specific rows by using the collapseRow method of Tree Grid by passing the row detail.
Index.cshtml
@model List<MVCTreeGrid.Controllers.TreeGridItems>
@Html.EJS().TreeGrid("TreeGrid").DataSource((IEnumerable<object>)Model).Columns(col =>
{
col.Field("TaskId").HeaderText("Task ID").Width(70).IsPrimaryKey(true).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
col.Field("TaskName").HeaderText("Task Name").Width(160).Add();
col.Field("StartDate").HeaderText("Start Date").Format("yMd").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width(90).Add();
col.Field("Duration").HeaderText("Duration").Width(80).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
}).ChildMapping("Children").TreeColumnIndex(1).DataBound("ondataBound").Collapsed("collapsed").Expanded("expanded").Render().
<script>
let currentData = [];
function collapsed(args) {
currentData.push(args.data);
store(args);
//storing collapsed record in local storage using setItem method
}
function expanded(args) {
currentData.push(args.data);
store(args);
//storing collapsed record in local storage using setItem method
}
function store(args) {
var treegrid = document.getElementById("TreeGrid").ej2_instances[0];
currentData.push(currentData.push(...treegrid.getCurrentViewRecords().filter((i)
=> i.hasChildRecords).filter((i) => !i.expanded)));
currentData = Array.from(new Set(currentData));
window.localStorage.setItem('currentData',JSON.stringify(currentData));
}
function ondataBound(args) {
if (this.initialRender) {
//checking whether it is initial rendering
var data = JSON.parse(window.localStorage.getItem('currentData'));
//retriving collapsed record in local storage using getItem method
if (data) {
var completeData = this.grid.dataSource;
var primaryKeyFieldName = this.getPrimaryKeyFieldNames()[0];
for (var i = 0; i < data.length; i++) {
var value;
for (var j = 0; j < completeData.length; j++) {
if (completeData[j][primaryKeyFieldName] == data[i][primaryKeyFieldName] ) {
value = completeData[j];
}
}
this.collapseRow(null, value); //collapsing row using collapseRow method
}
this.currentData = [];
window.localStorage.setItem('currentData',JSON.stringify(this.currentData));
}
}
}
</script>

Conclusion
I hope you enjoyed learning about how to maintain expand/collapsed state of rows in ASP.NET MVC TreeGrid.
You can refer to our ASP.NET MVC TreeGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET MVC TreeGrid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!