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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to get Grid layout with dynamic columns and data

Thread ID:





141214 Nov 29,2018 10:44 PM UTC Nov 30,2018 07:41 AM UTC Angular - EJ 2 1
Tags: Grid
Asked On November 29, 2018 10:44 PM UTC

Here is my Data Service
import { HttpClient, HttpParams, HttpHeaders } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { map } from "rxjs/operators";
import { Request } from "@angular/http";

export class DataService {

  constructor(private http: HttpClient) {


  public compData: Object[];
  public compColumns: CompData[];
  loadCompColumns() {
    return this.http.get("/api/CompDetails/GetColumns")
      map((data: any[]) => {
        this.compColumns = data;
        return true;
  loadCompData() {
    return this.http.get("/api/CompDetails/GetData")
        map((data: any[]) => {
          this.compData = data;
          return true;
export interface CompData {
  field: string,
  headerText: string,
  minWidth: number,
  maxWidth: number,
  width: number,
  allowEditing: Boolean,
  allowFiltering: Boolean,
  allowReordering: Boolean,
  allowResizing: Boolean,
  allowSorting: Boolean,
  isFrozen: Boolean,
  visible: Boolean,
  format: string,
  columns: CompData

Here is my home.component.html
<ejs-grid #gridComp height='500px' [editSettings]="editSettings" [toolbar]="toolbar" showColumnChooser='true' allowResizing= 'false' allowTextWrap='true' [gridLines]='lines' (created)="gridCreated($event)" >

Here is my home.component.ts file 
import { Component, OnInit, ViewChild, ViewEncapsulation, Input, Injectable, Inject, ElementRef } from '@angular/core';
import { DataManager, Query, UrlAdaptor, WebApiAdaptor, ODataAdaptor } from '@syncfusion/ej2-data';
import { GridComponent, EditService, ToolbarService, PageService, ColumnChooserService, EditSettingsModel, ToolbarItems, GridLine, Column, valueAccessor, ValueAccessor } from '@syncfusion/ej2-ng-grids';
import { EventArgs } from '@syncfusion/ej2-navigations';
import {DataService, AttorneyCompData} from '../shared/dataService'
  selector: 'app-home',
  styleUrls: ['./home.component.css'],
  templateUrl: './home.component.html',
  encapsulation: ViewEncapsulation.None,
  providers: [ToolbarService, EditService, PageService, ColumnChooserService]


export class HomeComponent implements OnInit {
  public title: string = 'Attorney Comp';
  public lines: GridLine = <GridLine>'Both';
  public editSettings: EditSettingsModel;
  public toolbar: ToolbarItems[];
constructor(private data: DataService) {

public columns: Object[];
  public gridAttorneyComp: GridComponent;
ngOnInit() {
this.data.loadCompData().subscribe(successForData => {
          if (successForData) {

            this.gridComp.dataSource = this.data.compData;
            this.gridComp.allowPaging = false;
            this.gridComp.pageSettings = { pageSize: this.data.compData.length };

            this.data.loadCompColumns().subscribe(success => {
              if (success) {
                this.columns = this.data.compColumns;
                this.gridComp.columns = this.columns;

public gridCreated(test: EventArgs) {


This keeps on generating error - 

if I hardcopy the columns that are returned from the web service in this.columns and assign it to the grid columns it works perfectly. But as soon as I go back to the web service to pull that data it errors out. I cannot use DataManager since I am doing some manipulation on columns on the grid and have to update one column at a time.

any help is greatly appreciated.

Mohammed Farook J [Syncfusion]
Replied On November 30, 2018 07:41 AM UTC

Hi Ameet, 
Thanks for contacting Syncfusion support. 
From schema definition the Grid can be rendered based on column or dataSource. So you must be define columns or dataSource for grid render. For your requirement we have created a sample “Grid with dynamic column render”. Please find the code example and sample for your reference. 
    <ejs-grid [dataSource]='data' allowPaging='true' height=365 [pageSettings]='initialPage' [columns] = 'columns' > 
. . . 
export class PageComponent implements OnInit { 
  public data: Object[]; 
  public initialPage: Object; 
  public columns: Object[]; 
  ngOnInit(): void { 
    this.data = data; 
    this.initialPage = { pageCount: 4 }; 
    this.columns = [ 
      { field: 'OrderID', headerText: 'Order ID', width: 120, textAlign: 'Right' }, 
      { field: 'CustomerName', headerText: 'Customer Name', width: 150 }, 
      { field: 'OrderDate', headerText: 'Order Date', width: 130, format: 'yMd', textAlign: 'Right' }, 
      { field: 'Freight', width: 120, format: 'C2', textAlign: 'Right' }, 
      { field: 'ShipCountry', headerText: 'Ship Country', width: 150 } 
Here we can define the dataSource and define the column in dynamic. 
Please get back to us, if you need any further assistance. 
J Mohammed Farook 


This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon