We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

TreeGrid | Dynamic columns possibilities

Hello,

after brief exploration of examples and documentation I figured out that Tree Grid in ASP.NET MVC (not WPF) can be rendered only by specifying exact (static columns) defined in some class (eg. viewModel).

For example including this in Razor page:

@(Html.EJ().TreeGrid("TreeGridContainer")
             .ChildMapping("Children")
             .TreeColumnIndex(1)
             .EnableResize(true)
             .Columns(co =>
               {
                   if (ViewBag.treeGridDataSource != null)
                   {
                       co.F
                       co.Field("TaskId").HeaderText("Task Id").Width(45).Add();
                   }
                   
                   co.Field("TaskName").HeaderText("Task Name").Add();
                   co.Field("StartDate").HeaderText("Start Date").Add();
                   co.Field("EndDate").HeaderText("End Date").Add();
                   co.Field("Duration").HeaderText("Duration").Add();
                   co.Field("Progress").HeaderText("Progress").Add();
               }
             )
             .SizeSettings(ss => ss.Width("100%").Height("600px"))
             .Datasource(ViewBag.treeGridDataSource)
        )
However I want to render columns dynamically according to user settings. Not only hide or show specific (earlier specified, hard typed) columns. For example I want to write something like foreach(var ViewBag.ColumnList){ co.Field("ColumnListItem.Name").HeaderText("ColumnListItem.HeaderText").Add(); }

Is it somehow possible to generate dynamic columns in Tree Grid for ASP.NET MVC?

Thanks all,
Martin Rasovsky







1 Reply

MK Mahalakshmi Karthikeyan Syncfusion Team March 1, 2016 11:56 AM UTC

Hi Martin,

Thanks for contacting Syncfusion support.

We can render dynamic columns based on the conditions in ASP.Net MVC TreeGrid as you have done in your code example. Please refer the below code example for details.

@(Html.EJ().TreeGrid("TreeGridContainer")

//…

.Columns(co =>

               {

                   if (ViewBag.dataSource == null)

                   {

                       co.Field("Id").HeaderText("Task Id").Width(45).Add();

                   }                  

                   co.Field("Name").HeaderText("Task Name").Add();

                   co.Field("StartDate").HeaderText("Start Date").Add();

                   co.Field("Duration").HeaderText("Duration").Add();

                   co.Field("PercentDone").HeaderText("Progress").Add();

               }

             )

     .Datasource(ViewBag.dataSource)

    )


We have also prepared a sample based on this and you can find the sample under the following location.

Sample: http://www.syncfusion.com/downloads/support/directtrac/general/ze/DynamicColumn2079197477

Note: We have removed bin and obj folder in the given sample for some security reasons, we must include Syncfusion.EJ and Syncfusion.EJ.MVC dlls to render the TreeGrid control which is available in Essential studio installed location

Regards,

Mahalakshmi K.


Loader.
Live Chat Icon For mobile
Up arrow icon