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
niharnihar 

how to delete rows in pageblocktable by using deleterow button in apexclass my code is below

apexclass:
public  class EnrollmentFormClass {
public Contact primarycontact{get;set;}
public Contact othercontact{get;set;}
public Contact childcontact{get;set;}
public List<Contact> concAddList{get;set;} 
public Account acc{get;set;}
    
public EnrollmentFormClass() {
     primarycontact  = new Contact();
     othercontact = new Contact();
     childcontact = new Contact(); 
     acc  = new Account();
     concAddList  = new List<Contact>();   
     concAddList.Add(new Contact());
     }
    
public void AddRow(){
     childcontact = new Contact();
     concAddList.Add(childcontact);
       
     }
public void save(){
     insert  primarycontact;   
     acc.name = primarycontact.lastName; 
     insert acc;     
     primarycontact.accountId = acc.id;
     upsert primarycontact;
     othercontact.accountId = acc.id;
     othercontact.Contact1__c = primarycontact.Id; 
     insert othercontact;    
     for(contact cc :concAddList){
     cc.accountId = acc.id;
     cc.Contact1__c = primarycontact.Id;
     
     }
     upsert concAddList;
     }
     }
visualforcepage:
<apex:page controller="EnrollmentFormClass" showHeader="false" sidebar="false">
    <apex:form >
    <apex:pageBlock title="Opportunitiy Zone Enrollment Form">
              <apex:pageBlockSection columns="3">
              <apex:inputField value="{!primarycontact.Student_Name__c}"/>
              <apex:inputField value="{!primarycontact.School__c}"/> 
              <apex:inputField value="{!primarycontact.Date__c}"/>    
            </apex:pageBlockSection>     
            <br/> 
            <br/>
                    SPARC programs are funded by United Way of Greater Atlanta. As such, we are required to report demographic, income, educational and health access information on every individual/family we serve. With the United Way belief that serving one family member serves them all, we collect general information on each member of your household. We appreciate your willingness to complete this form in its entirety so that we can continue providing free programs to the community. Thank you!
            <br/>  
            <br/> 
            <apex:pageBlockSection title="Head of Household Information:">
                <apex:inputField value="{!primarycontact.FirstName}"/>
                <apex:inputField value="{!primarycontact.LastName}"/>
                <apex:inputField value="{!primarycontact.Date_of_Birth__c}"/>
                <apex:inputField value="{!primarycontact.Gender__c}"/> 
                <apex:inputField value="{!primarycontact.Do_you_have_a_primary_medical_provider__c}"/>
                <apex:inputField value="{!primarycontact.Do_you_have_a_primary_medical_provider__c}"/>
                <apex:inputField value="{!primarycontact.Address__c}"/>
                <apex:inputField value="{!primarycontact.City__c}"/>
                <apex:inputField value="{!primarycontact.State__c}"/>
                <apex:inputField value="{!primarycontact.County__c}"/>
                <apex:inputField value="{!primarycontact.Phone__c}"/>
                <apex:inputField value="{!primarycontact.Email_Address__c}"/>
                <apex:inputField value="{!primarycontact.Preferred_Method_of_Contact_Circle_One__c}"/>
            </apex:pageBlockSection>
            <apex:pageBlocksection title="Demographic Data: (All information is confidential and is used in applying for grants to fund this program.)" columns="3">
                 <apex:inputField value="{!primarycontact.Spanish_translation_Services_Needed__c}"/>
                 <apex:inputField value="{!primarycontact.Race_Check_One__c}"/>
                 <apex:inputField value="{!primarycontact.Marital_Status_check_one__c}"/>
                 <apex:inputField value="{!primarycontact.HIGHEST_LEVEL_OF_EDUCATION_Check_one__c}"/>
                 <apex:inputField value="{!primarycontact.Employment_check_one__c}"/>
            </apex:pageBlocksection>
            <apex:pageBlocksection title="Income:" columns="3">
                <apex:inputField value="{!primarycontact.Annual_Income__c}"/> 
                <apex:inputField value="{!primarycontact.Source_of_Income__c}"/> 
                <apex:inputField value="{!primarycontact.Primary_Method_of_Transportation__c}"/>
            </apex:pageBlocksection>
            <apex:pageBlocksection title="HOUSEHOLD Information:" columns="3">
                <apex:inputField value="{!othercontact.Other_Adult_First_Name__c}"/>
                <apex:inputField value="{!othercontact.LastName}"/>
                <apex:inputField value="{!othercontact.Date_of_Birth__c}"/>
                <apex:inputField value="{!othercontact.Gender__c}"/> 
                <apex:inputField value="{!othercontact.Relationship_Primary_Care_Provider__c}"/>
                <apex:inputField value="{!othercontact.Medical_Insurance__c    }"/>  
            </apex:pageBlocksection>
               
            <apex:pageBlocksection title="Children (all children currently in your household, INCLUDING those participating in this program):" columns="1">
             <apex:pageBlockTable value="{!concAddList}" var="c">
             <apex:column headerValue="Name">
                 <apex:commandButton value="Add Row" action="{!addRow}" />
             
             </apex:column>
             <apex:column headerValue="Name"> 
             <apex:inputfield value="{!c.lastName}"/>    
             </apex:column> 
                    
       </apex:pageBlockTable> 
       </apex:pageBlocksection>
         <apex:commandButton value="Save" action="{!save}"/>
       </apex:pageBlock>
    </apex:form>
</apex:page>
Best Answer chosen by nihar
SandhyaSandhya (Salesforce Developers) 
Hi,

You can add below method to your code.

public void delRow()
    {
        rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
        memberAddList.remove(rowNum);   
    }    

Also refer below links for sample code.

http://www.infallibletechie.com/2012/06/dynamically-deleting-rows-in-apex.html
 
http://www.forcetree.com/2015/03/dynamically-addremove-rows-from-table.html
 
If this helps you, please mark it as solved so that it will be available for others as a solution.

Best Regards,
Sandhya

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

You can add below method to your code.

public void delRow()
    {
        rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
        memberAddList.remove(rowNum);   
    }    

Also refer below links for sample code.

http://www.infallibletechie.com/2012/06/dynamically-deleting-rows-in-apex.html
 
http://www.forcetree.com/2015/03/dynamically-addremove-rows-from-table.html
 
If this helps you, please mark it as solved so that it will be available for others as a solution.

Best Regards,
Sandhya
This was selected as the best answer
niharnihar
hi sandhya,
                I refered your links which was shared by you thanks for that. But i cannot implement on my code so try to implement on my code sandhya................ 
kiran jain 14kiran jain 14
Thanks