[SalesForce] Conditionally Render Apex:Column

I am trying to set up a VF page so that some of the columns only show when they have data. My VF page uses a custom list controller. I have tried using a rendered attribute in the column tag, and while that works when there is no data, if one of my rows has data the column header attribute remains blank. Does anyone know how I can accomplish this?

VF Page:

<apex:page Controller="CalculateQLISummary" action="{!init}" tabStyle="Quote__c">
    <apex:pageMessages id="pgMess" />

    <apex:form> 
        <apex:pageBlock  title="Summary">
            <center><apex:commandButton title="Back" value="Back" action="{!cancel}"/></center><br/><br/>

        <apex:outputText value="Summary" /><p/>
            <apex:pageBlockTable title="Grouping Summary" value="{!listQLI2}" var="OLI2" style="width:100%">
                <facet name="Grouping"/>
                <apex:column headerValue="Product" style="width:30%">
                    <apex:outputField value="{!OLI2.Name}"/>
                </apex:column>
                <apex:column headerValue="List Price" rendered="{!IF(OLI2.List_Price__c != NULL,true,false)}">
                    <apex:outputField value="{!OLI2.List_Price__c}"/>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Best Answer

Visualforce expressions don't support a syntax like what you seem to need:

<apex:column headerValue="List Price" rendered="{!ISFIELDPOPULATEDINANY(listQLI2, List_Price__c)}">
    <apex:outputField value="{!OLI2.List_Price__c}"/>
</apex:column>

OLI2 here is your iteration variable, which will take on the value of each entry in listQLI2 as Visualforce renders the table. It can't act as a sort of proxy for the value of a field across the whole data set.

You have to drop to Apex to implement that summarization of the underlying list data. You could simply implement this as a Boolean property:

public Boolean anyListPricePopulated {
    get { 
        for (QuoteLineItem qli : listQLI2) {
            if (qli.ListPrice != null) {
                return true;
            }
        }
        return false;
    }
}

Then, in Visualforce,

<apex:column headerValue="List Price" rendered="{! anyListPricePopulated  }">
    <apex:outputField value="{!OLI2.List_Price__c}"/>
</apex:column>