[SalesForce] Display All records from dropdownlist

I have a page where I'm displaying List of records of an object (Product) on selecting each record from a dropdown list (default value —————–Select———-). My requirement is the moment I open the page (with default option of ———-Select———- on dropdown list), I need to display all the records on my page. Currently once I open the page, it shows no records. but once I select any value from dropdown list and then click ———-Select———–, it shows all the records. I need the same functionality when Im opening the page. In addition to this Im also trying to add 'GROUP BY' clause to my query on Category__c field which is of Text type, but its not allowing me to group it. Gives an error which says "Field must be grouped or aggregated: Id ". I tried adding ORDER BY ID but it again gave me an error "Ordered field must be grouped or aggregated: Id". Please help. Below is my code:

Controller-

public class MyController{    
Public String selectedName {get;set;}
Public String newPCName{get;set;}
Public Product2 objPC{get;set;}    
Public List<Wrapper> wrapList{get;set;}
Public List<Product2> pcList{get;set;}
public Boolean flag{get;set;}

Public MyController(){
    flag =  true;
  objPC = new Product2(); 
}


public List<SelectOption> getProductList(){
    List<SelectOption> ProductList = new List<SelectOption>();
    ProductList.add(new SelectOption('------------Select-----------','------------Select-----------')); 
    List<Product2> prodList = [Select id, name, Category__c, FieldA__c from Product2];
    for(Product2 pc :prodList){
        ProductList.add(new SelectOption(pc.Id,pc.Name));     
    }
   return ProducttList;    
}

Public void showrecord(){
    System.debug('=====selectedName=====' + selectedName);
    wrapList = new List<Wrapper>();
    if(selectedName == '------------Select-----------'){
    pcList = [select id, Name, Category__c, FieldA__c, Description__c  from Product2];
}
else{

   pcList = [select id, Name, Category__c, FieldA__c, Description__c,   from Product2 where id =: selectedName];
    }     
    for(Product2 pc :pcList)
    {
        wrapList.add(new Wrapper(false,pc));
    }        
    system.debug('inmethod'+ pcList);
}
Public Class Wrapper{
    Public Boolean ischeck{get;set;}
    Public Product2 prodcat{get;set;}
    Public Wrapper (Boolean ischeck, Product2 prodcat){
        This.ischeck=ischeck;
        This.prodcat=prodcat;
    }    
}

 }

Page-

<apex:page controller="MyController" sidebar="false" action="{!showrecord}">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection title="Product" rendered="{!flag}">
            <apex:outputLabel value=""/>
            <apex:selectlist value="{!selectedName}" size="1" id="values"> 
                <apex:SelectOptions value="{!ProductList}"/>  
                <apex:actionSupport event="onchange" action="{!showrecord}" reRender="pc"/>          
            </apex:selectlist>
            <apex:pageBlockTable value="{!wrapList}" var="v" id="pc" style="align :center;-moz-border-radius: 10px;-webkit-border-radius: 10px;border-radius: 10px;width: 225%;">                 
                <apex:column value="{!v.prodcat.Category__c}" width="10%"/>
                <apex:column value="{!v.prodcat.FieldA__c}" width="10%"/>                    
                <apex:column value="{!v.prodcat.Description__c}" width="60%"/>  

            </apex:pageBlockTable>
        </apex:pageBlockSection>  
        <apex:pageBlockSection rendered="{!NOT(flag)}">

        </apex:pageBlockSection>        
    </apex:pageBlock>
</apex:form>

Best Answer

Adding:

selectedName = '------------Select-----------';
showrecord();

at the end of your constructor will display the records when the page opens.

The problem you are seeing when trying to add GROUP BY is that you must apply an aggregate function to every other field returned by the query. That is because when multiple records have the same value of the field you are grouping by, you need to nominate which of the multiple values from the non-grouped fields to show. The names of the data values returned also change. See Working with SOQL Aggregate Functions too.