function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

How to Represent a Dynamic query in a PageBlockTable or a Grid in Visualforce

I was preparing this page where I need to Show All fields of Account object and some selected fields from that list Should only should be queried from the Account object. All is resolved till now that I face one major hurdle. Now I need to represnt this Dynamically Queried Account List in a table or Grid in the page itself. The problem lies in the fact that the queried columns names are not known to me but in a " Set<String> rightvalues " so the <apex:pageBlockTable value="{!accList}" var=" dispAcc"> <apex:column value="{!dispAcc.?}> .....How do I resolve this?


my code is as given under this following


The Controller :

public with sharing class GridRep {

public list<Account> accList = new list<Account>();
public list<Account> getAccList()
return accList;
// public list<Account> accList{get;set;}
public list<String> availableFields{get;set;}
public list<String> selectedFields {get;set;}
public Set<String> leftvalues = new Set<String>();
public Set<String> rightvalues=new Set<String>();
public String strQuery;

public GridRep(){
availableFields = new List<String>();
selectedFields = new List<String>();

public List<String> getFieldList(Map<String, Schema.SObjectField> fieldMap)
//Make a comma-separated list of the fields
List<String> fieldList =new List<String>();
for(String fieldName:fieldMap.keySet())
Schema.DescribeFieldResult fieldDescribe = fieldMap.get(fieldName).getDescribe();

return fieldList;

public PageReference refreshPage()
availableFields = new List<String>();
Map<String, Schema.SObjectField> fieldMap=new Map<String, Schema.SObjectField>();
fieldMap = Schema.SObjectType.Account.fields.getMap();
List<String> fieldNames=getFieldList(fieldMap);
for (String fldName:fieldNames)

return null;

public List<SelectOption> getunSelectedFields()
List<SelectOption> optionFields = new List<SelectOption>();
List<string> tempList = new List<String>();
for(string s : tempList)
optionFields.add(new SelectOption(s,s));
return optionFields;
public PageReference selectclick()
for(String s : availableFields)
return null;
public List<SelectOption> getSelectedValues()
List<SelectOption> optionsFields2 = new List<SelectOption>();
List<string> tempList2 = new List<String>();
for(String s : tempList2)
optionsFields2.add(new SelectOption(s,s));
return optionsFields2;

public PageReference unselectclick()

for(String s : selectedFields)

return null;

public pagereference find()
strQuery='Select ';
for(String temp:rightvalues)
strQuery+=' from Account';

accList=new list<Account>();
}catch(Exception e)
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Error in Database Fetch');

return null;



and the page as is :


<apex:page controller="GridRep" sidebar="false" >
<apex:form id="fm1">
<apex:sectionHeader title="Grid Like Representation"/>
<apex:commandButton action="{!refreshPage}" value="Populate Available"/>
<apex:pageBlock >
<apex:pageBlockButtons location="top">
<apex:commandButton value="Find" action="{!find}" id="button1"/>

<apex:panelGrid columns="3" id="abcd">
<apex:selectList value="{!availableFields}" multiselect="true" style="height:80px;width:150px" >
<apex:selectOptions value="{!unSelectedFields}" />
<apex:panelGroup >
<apex:commandButton value="Transfer Right" action="{!selectclick}" reRender="abcd"/>
<apex:commandButton value="Transfer Right" action="{!unselectclick}" reRender="abcd"/>
<apex:selectList value="{!selectedFields}" multiselect="true" style="height:80px;width:150px" >
<apex:selectOptions value="{!SelectedValues}" />
<apex:pageBlockSection >
<!--   Here Should Be the code to Display Dynamic queried list accList -->


Jake GmerekJake Gmerek

You have to use dynamic Visualforce for that.  Here is an example from something similar that I did with related lists, but the concept is the same in that I needed to set the number and type of columns dynamically: