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

Add Columns Programatically Before They Are Auto Generated

Hi Syncfusion,

I could probably add columns something like this at any time:
var colObj = { headerText: "Ship City", field: "ShipCity", width: 120 };  
gObj.columns.push(colObj);
gObj.refreshColumns(); 

But I would like to add columns programatically but as early as possible. So basically add this programatically at run time:
https://www.screencast.com/t/bk3sqtImeyk

Would it be possible to add this att the data grid's loaded or created event? And if so what is the best way to add it?

Thanks,
Marius

1 Reply

BS Balaji Sekar Syncfusion Team January 7, 2020 09:15 AM UTC

Hi Marius Lian, 
 
Greetings from Syncfusion Support. 
 
Query #1: Would it be possible to add column to Data Grid in Load or Created event ? 
 
We have analyzed your query and we can understand that you need to push columns to Grid as early as possible. Yes, you can add columns to Grid in the Load or Created events of Grid. We suggest you to use Load event. This event allows customization of Grid properties before rendering. We have achieved your requirement and created a sample based on your query. Please refer the below code snippet and sample link for more information. 
 
App.vue 
 
<template> 
  <div id="app"> 
    <ejs-grid 
      id="Grid" 
      ref="grid" 
      :dataSource="data" 
      :allowPaging="true" 
      :editSettings="editSettings" 
      :load="load" 
    ></ejs-grid> 
  </div> 
</template> 

<script> 
import Vue from "vue"; 
import { GridPlugin, Edit, Page, Toolbar } from "@syncfusion/ej2-vue-grids"; 
import { data } from "./datasource"; 

Vue.use(GridPlugin); 

export default { 
  data() { 
    return { 
      data: data, 
      editSettings: { 
        allowAdding: true, 
        allowEditing: true, 
        allowDeleting: true 
      }, 
      toolbar: ["Add", "Edit", "Delete", "Update", "Cancel"] 
    }; 
  }, 
  methods: { 
    load: function()
      var colObj = { headerText: "Ship City", field: "ShipCity", width: 120 }; 
      this.$refs.grid.ej2Instances.columns.push(colObj); 
    } 
  }, 
  provide: { 
    grid: [Page, Edit, Toolbar] 
  } 
}; 
</script> 

<style> 
</style> 
 
 
 
                                       https://ej2.syncfusion.com/documentation/api/grid/#created 
 
If we have misunderstood your query, please provide us more details that will be helpful for us to provide you better assistance. 
  1. Please share the full Grid rendering code.
 
Please get back to us if you need further assistance. 
 
Regards, 
Balaji Sekar. 


Loader.
Up arrow icon