列表视图项目未清除 [英] Listview items not clearing

查看:62
本文介绍了列表视图项目未清除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在使用C#,。net 2.0;



我正在服用用户输入并使用ListView Control存储和显示。我需要提供删除行,清除所有行,向此列表视图添加行。



当用户选择一行并单击删除按钮时,所选行将为删除。当用户点击清除按钮时,我需要清除列表视图中的所有行。



我使用以下代码。



问题: - 当我使用删除时,清除按钮项目仅从查看清除,但不从列表视图实例清除。这意味着当我尝试访问用户的输入时,它会显示以前的结果。



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.

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());
           }
       }

推荐答案

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());
           }
       }


这篇关于列表视图项目未清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆