4D Blog

Home Product 4D View Pro: Populate your sheets with a data context

4D View Pro: Populate your sheets with a data context

April 26, 2022

Product

4D View Pro offers you a new way to load your data and create your templates: the data context. It allows you to create your document with placeholders and fill them by setting this data context.

With just a few lines of code, you can display objects or collections without authorizing them first with VP SET CUSTOM FUNCTION! Let’s delve into the details. 

HDI 4DVP Data Context

Create a document with placeholders

First, you need to create a document with your object attributes. For example, if you use an object with 2 attributes, Firstname and Lastname, you can create two placeholders with the VP SET BINDING PATH command:

VP SET BINDING PATH(VP Cell("ViewProArea"; 0; 0); "Firstname")
VP SET BINDING PATH(VP Cell("ViewProArea"; 0; 1); "Lastname")

No values appear in your sheet because the data context is not yet defined. To verify that your cells are correctly bound, go to the DATA tab of the Ribbon interface and click on the Sheet Binding button. Your attributes’ names will appear between brackets:

 

Data context

Now that your template is ready, just set your data context with your object using VP SET DATA CONTEXT:

$object:=New object
$object.Firstname:="John"
$object.Lastname:="Smith"
VP SET DATA CONTEXT("ViewProArea"; $object)

So the template is updated automatically:

blank

Of course, the user can modify the data:

blank

In this case, no problem; just use the VP Get data context method to retrieve your object with the modifications:

$object:=VP Get data context("ViewProArea")

//$object={Firstname:Judith,Lastname:Smith}

Data injection in sheetS

If you pass a collection to your data context, this collection will be automatically bound to the sheet. If the autoGenerateColumns option of the VP SET DATA CONTEXT command is set to true, the collection is automatically displayed in the sheet.

For example, if you want to display a database table, just write:

$people:=ds.People.all().toCollection()

VP SET DATA CONTEXT("ViewProArea"; $people; New object("autoGenerateColumns"; True)

You obtain:

blank

The number of columns and rows are automatically calculated, and the columns’ names are the table’s field names.

Note that if autoGenerateColumns is not set to true, no data is displayed!

Check out the HDI above to see these commands in action. Also, make sure to read the documentation for more details!

Discuss

Tags 4D View Pro, Data context, Document creation, Spreadsheet, v19 R5, v20

Latest related posts

  • July 18, 2025

    Embed deployment license automatically into your applications

  • July 17, 2025

    New Class to Perform UDP Communications

  • July 17, 2025

    Managing Calendar Events with Microsoft 365 or Google in 4D: Create, Update, Delete

Fabrice Mainguené
Fabrice Mainguené
• Product Owner •Fabrice Mainguené joined 4D Program team in November, 2016. As a Product Owner, he is in charge of writing the user stories then translating it to functional specifications. His role is also to make sure that the feature implementation delivered is meeting the customer need.After obtaining a Bachelor degree in Computer Science at CNAM, Fabrice joined a small software publishing company as a Windev developer. Then he worked for different companies in industry and trade areas as a Windev and web developer as well as technical advisor on new features.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • 4D View Pro
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R9 20 R10 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Google Listbox Logs Mail Microsoft 365 Network Objects ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R9 20 R10 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Google Listbox Logs Mail Microsoft 365 Network Objects ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor
Subscribe to 4D Newsletter

© 2025 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy