Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I am using C#, .net 2.0;

I am taking input from user and storing and showing that using ListView Control. I need to provide Delete a row, Clear all Rows, Add a rows to this List view.

When user selects a row and clicks on Delete button selected rows will be deleted. When user clicks on clear button i need to Clear all the rows from List view.

I am using following code.

Problem :- When i use Delete, Clear button items are clearing from View only but not from listview instance. Which means when i am trying to access input from user it is showing along with previous results.

C#
private void btnClear_Click(object sender, EventArgs e)
       {
           try {
               if (lstViewCPTCompliance.Items.Count > 0)
               {
                   lstViewCPTCompliance.Items.Clear();
                   lstViewCPTCompliance.Update();
                   lstViewCPTCompliance.Refresh();
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message.ToString());
           }
       }

       private void btnDelete_Click(object sender, EventArgs e)
       {
           try {
               for (int i = 0; i < lstViewCPTCompliance.Items.Count; i++)
               {
                   if (lstViewCPTCompliance.Items[i].Selected == true)
                   {
                       lstViewCPTCompliance.Items[i].Remove();
                       lstViewCPTCompliance.Update();
                       lstViewCPTCompliance.Refresh();
                   }

               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message.ToString());
           }
       }
Posted
Updated 6-Jul-16 19:56pm
v2
Comments
Member 12346239 6-Jul-16 9:32am    
Are you want to clear/ delete all row from list instance ?
Charles Shob 6-Jul-16 9:50am    
Yes.
Philippe Mori 6-Jul-16 15:11pm    
Don't repeat same code twice.
Charles Shob 6-Jul-16 19:33pm    
ok

1 solution

C#
private void btnClear_Click(object sender, EventArgs e)
       {
           try {
               for (int i = 0; i < lstViewCPTCompliance.Items.Count; i++)
               {                   
                       lstViewCPTCompliance.Items[i].Remove();
                       lstViewCPTCompliance.Update();
                       lstViewCPTCompliance.Refresh();
                  
               }
           }
        
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message.ToString());
           }
       }
 
       private void btnDelete_Click(object sender, EventArgs e)
       {
           try {
               for (int i = 0; i < lstViewCPTCompliance.Items.Count; i++)
               {
                   if (lstViewCPTCompliance.Items[i].Selected == true)
                   {
                       lstViewCPTCompliance.Items[i].Remove();
                       lstViewCPTCompliance.Update();
                       lstViewCPTCompliance.Refresh();
                   }
 
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message.ToString());
           }
       }
 
Share this answer
 
v3
Comments
Philippe Mori 6-Jul-16 15:12pm    
Use code block. This is unreadable and don't even worth one point if not formatted. If you don't make the effort to format it, then I won't make the effort to read it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900