The <apex: column> component is one of the most important component when we work with <apex: dataTable> and <apex:pageBlockTable> components. The <apex: column> is used for creating a single column in a table and it must always be child of <apex:dataTable> or <apex:pageBlockTable> components.
ApexColumnExample.vfp
<!-- create apex page with custom controller-->
<apex:page controller="ApexColumnController">
<!-- use apex form to show the accounts and contacts table-->
<apex:form >
<apex:pageBlock title="Account/Contact Information">
<!-- command button that is responsible for showing accounts in the table-->
<apex:commandButton action="{!getAccounts}" value="Show Accounts"/>
<!-- command button that is responsible for showing contacts in the table-->
<apex:commandButton action="{!getContacts}" value="Show Contacts"/>
<apex:pageBlockSection rendered="{!IF(accounts.size>0, true, false)}">
<!-- create a table for showing accounts information -->
<apex:pageBlockTable value="{!accounts}" var="acc">
<apex:column value="{!acc.Name}"/>
<apex:column value="{!acc.AccountNumber}"/>
<apex:column value="{!acc.Phone}"/>
<apex:column value="{!acc.Rating}"/>
<apex:column value="{!acc.Website}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
<apex:pageBlockSection rendered="{!IF(contacts.size>0, true, false)}">
<!-- create a table for showing contacts information -->
<apex:pageBlockTable value="{!contacts}" var="con">
<apex:column value="{!con.FirstName}" />
<apex:column value="{!con.LastName}"/>
<apex:column value="{!con.AccountId}"/>
<apex:column value="{!con.Email}"/>
<apex:column value="{!con.Phone}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
ApexColumnController.apxc
// create ApexColumnController class to get the list of Accounts and Contacts
public class ApexColumnController {
public List<account> accounts{get;set;}
public List<contact> contacts{get;set;}
// default constructor
public ApexColumnController(){
accounts = new List<account>();
contacts = new List<contact>();
}
// create getAccounts() method to get account records
public void getAccounts() {
accounts = [select Id, Name, Rating, Phone, AccountNumber, Website From Account];
}
// create getContacts() method to get contact records
public void getContacts() {
contacts = [select Id, FirstName, LastName, AccountId, Email, Phone From Contact];
}
}