简体   繁体   English

从ListView中删除项目并在C#中列出

[英]Delete Items from ListView and List in C#

I worked out how to delete an item from my listview with this code: 我想出了如何使用以下代码从列表视图中删除项目:

private void buttonDelete_Click(object sender, EventArgs e) {
    if (listViewStudents.SelectedItems != null) {
        foreach (ListViewItem eachItem in listViewStudents.SelectedItems) { 
            listViewStudents.Items.Remove(eachItem);
            ClearandFocus();
        }
    }
}

But as you can see in the code below every time input gets added into the ListView it also gets added into a collection list with Repository.AddStudent(student) 但是正如您在下面的代码中看到的那样,每次将输入添加到ListView它也会使用Repository.AddStudent(student)添加到集合列表中。

private void buttonAdd_Click(object sender, EventArgs e) {
     Student student = GetStudent();
     Repository.AddStudent(student);
     string[] row = { student.LastName, student.StreetAddress, student.StreetNumber, student.Box, student.Zip, student.City,
                      student.Country, student.Birthday, student.Birthplace, student.Gender, student.IDNumber, student.MaritalStatus };
     listViewStudents.Items.Add(student.FirstName.ToString()).SubItems.AddRange(row);
     ClearandFocus();
 }

What I would like to achieve is as following: every time the delete button gets clicked I would like to have the selected item deleted in the listview AND the list. 我要实现的目标如下:每次单击删除按钮时,我希望在列表视图和列表中删除所选项目。 I've done serveral attempts but can't seem to get this done, I wonder if this is even possible? 我已经进行过服务器尝试,但似乎无法完成,我想知道是否有可能?

Synchronizing two lists is not trivial and doing so amually is not recommended. 同步两个列表并不是一件容易的事,因此不建议您这样做。

Consider making your list of Students an ObservableCollection<> and set it as the ItemsSource of your ListView . 考虑将学生列表设置为ObservableCollection<>并将其设置为ListViewItemsSource Changes in the collection will be reflected in you ListView then. 然后,集合中的更改将反映在您的ListView

将 ListView 项目投射到列表<object>在 C#<div id="text_translate"><p> 在从列表视图中删除一个元素后,我需要将当前列表视图项存储在新的 object 列表中。</p><p> 这是我的 schema.cs</p><pre> public class Show { public class Show { public Show() { } public int OrdNum { get; set; } public DateTime DTshow { get; set; } public string values { get; set; } public int practice_Number { get; set; } } }</pre><p> 问题出在 Takenshows.cs</p><p> 从 listview 中删除元素后,我不知道如何将 listview 项转换为 List< Show>。</p><p> 这是我按下并从列表视图中删除现有元素的按钮:</p><pre> //Takenshows.cs... public List<Show> myShows; public TakenShows() { InitializeComponent(); lvwColumnSorter = new ListViewColumnSorter(); this.listView1.ListViewItemSorter = lvwColumnSorter; myShows = new List<Show>(); } private void button1_Click(object sender, EventArgs e) { c = 0; if (listView1.SelectedItems;= null) { for (int i = 0. i < listView1.Items;Count. i++) { if (listView1.Items[i].Selected) { DialogResult dr = MessageBox?Show("Are you sure you want to remove the element,", "WARNING". MessageBoxButtons,YesNo. MessageBoxIcon;Warning). switch (dr) { case DialogResult:Yes. listView1.Items[i];Remove(); i--; for (int j = 0. j < listView1.Items;Count; j++) { c = c + 1. listView1.Items[j].SubItems[0].Text = c;ToString(). } f = Int32.Parse(c;ToString()). // HERE's THE PROBLEM I need to cast my selected items from list view to object list ( List<Show>) and store those in myShows typed List<Show> Data myShows = listView1.SelectedItems.Cast<ListViewItem>().Select(x => x,OrdNum. x,DTshow. x,values. x.practice_Number);ToList(). var frm2 = Application.OpenForms.OfType<Main>();First(). if (frm2;= null) { frm2.devCont(); frm2;devcontlist(f). } break: case DialogResult;No: break; } } } } }</pre><p> 将不得不做这样的事情:</p><pre> //BUT this code not works myShows = listView1.SelectedItems.Cast<ListViewItem>().Select(x => x.OrdNum,x.DTshow,x.values,x.practice_Number).ToList();</pre><p> 当我从列表视图项中删除现有元素时,我需要在删除其中一个元素后保留的元素更新列表视图,而不包括我删除的元素。</p><p> 在我从 listview 中删除现有元素并且它必须存储在 < Show> 列表中后,listview 必须更新。 我怎样才能做到这一点? 我已经尝试了所有可能的方法,但这几乎是不可能的。</p></div></object> - Cast ListView Items to List<Object> in C#

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 C# 中从 ListView 中删除项目 - Delete Items from ListView in C# C# 将项目从列表添加到列表视图 - C# Adding items to Listview from List 如何使用删除按钮从ListView C#中删除多个项目 - How To Delete Multiple Items from ListView C# With Delete Button 如何从ListView组件c#中删除组中的所有项目 - How to delete all items from a group in ListView component c# 将 ListView 项目投射到列表<object>在 C#<div id="text_translate"><p> 在从列表视图中删除一个元素后,我需要将当前列表视图项存储在新的 object 列表中。</p><p> 这是我的 schema.cs</p><pre> public class Show { public class Show { public Show() { } public int OrdNum { get; set; } public DateTime DTshow { get; set; } public string values { get; set; } public int practice_Number { get; set; } } }</pre><p> 问题出在 Takenshows.cs</p><p> 从 listview 中删除元素后,我不知道如何将 listview 项转换为 List< Show>。</p><p> 这是我按下并从列表视图中删除现有元素的按钮:</p><pre> //Takenshows.cs... public List<Show> myShows; public TakenShows() { InitializeComponent(); lvwColumnSorter = new ListViewColumnSorter(); this.listView1.ListViewItemSorter = lvwColumnSorter; myShows = new List<Show>(); } private void button1_Click(object sender, EventArgs e) { c = 0; if (listView1.SelectedItems;= null) { for (int i = 0. i < listView1.Items;Count. i++) { if (listView1.Items[i].Selected) { DialogResult dr = MessageBox?Show("Are you sure you want to remove the element,", "WARNING". MessageBoxButtons,YesNo. MessageBoxIcon;Warning). switch (dr) { case DialogResult:Yes. listView1.Items[i];Remove(); i--; for (int j = 0. j < listView1.Items;Count; j++) { c = c + 1. listView1.Items[j].SubItems[0].Text = c;ToString(). } f = Int32.Parse(c;ToString()). // HERE's THE PROBLEM I need to cast my selected items from list view to object list ( List<Show>) and store those in myShows typed List<Show> Data myShows = listView1.SelectedItems.Cast<ListViewItem>().Select(x => x,OrdNum. x,DTshow. x,values. x.practice_Number);ToList(). var frm2 = Application.OpenForms.OfType<Main>();First(). if (frm2;= null) { frm2.devCont(); frm2;devcontlist(f). } break: case DialogResult;No: break; } } } } }</pre><p> 将不得不做这样的事情:</p><pre> //BUT this code not works myShows = listView1.SelectedItems.Cast<ListViewItem>().Select(x => x.OrdNum,x.DTshow,x.values,x.practice_Number).ToList();</pre><p> 当我从列表视图项中删除现有元素时,我需要在删除其中一个元素后保留的元素更新列表视图,而不包括我删除的元素。</p><p> 在我从 listview 中删除现有元素并且它必须存储在 < Show> 列表中后,listview 必须更新。 我怎样才能做到这一点? 我已经尝试了所有可能的方法,但这几乎是不可能的。</p></div></object> - Cast ListView Items to List<Object> in C# 循环删除列表中的多个项目…c# - Loop to delete multiple items in list…c# 如何删除列表C#中的一项? - How to delete one items in List C#? 从C#中的ListView中删除项目 - Removing items from a ListView in C# 如何在ASP.NET C#中删除特定的listview项目? - How to delete specific listview items in ASP.NET C#? C#WPF循环并删除选中的ListView项目 - C# WPF Loop & Delete Checked ListView Items
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM