BPC-Components

Home > Products > BPC PowerForms > Knowledge Base > Scripting > SetDataSource method for DataGrid

SetDataSource method for DataGrid

SetDataSource is a new method used to bind a DataGrid to an array of objects during runtime.

 

c_Control5 is our example datagrid.

Code

var c = form.GetControl("c_Control5").InputControl;

var data = [];
var item = new Object();
item.ID="1"; item.Name = "John"; data.push(item);
item = new Object();
item.ID="2"; item.Name = "George"; data.push(item);

c.SetDataSource(data);

 

Let's analyze the above code in more detail:

Code

var c = form.GetControl("c_Control5").InputControl;

The above segment finds our example datagrid and stores it in a variable. Normally we'd only have to use

var c = form.GetControl("c_Control5"); but in our case the SetDataSource command is under the InputControl property.

 

Code

var data = [];

Declares an array called data which we'll use to store objects and finally binds it to the datagrid.

 

Code

var item = new Object();
item.ID="1"; item.Name = "John"; data.push(item);

The above code segment creates a new object called item, declares two parameters called ID and Name and initializes them with values . data.push(item); stores the newly created object in the array.

 

Code

c.SetDataSource(data);

Finally, we use SetDataSource to bind the array to our example's datagrid. The result of our actions is shown below: