繁体   English   中英

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

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

我想出了如何使用以下代码从列表视图中删除项目:

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

但是正如您在下面的代码中看到的那样,每次将输入添加到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();
 }

我要实现的目标如下:每次单击删除按钮时,我希望在列表视图和列表中删除所选项目。 我已经进行过服务器尝试,但似乎无法完成,我想知道是否有可能?

同步两个列表并不是一件容易的事,因此不建议您这样做。

考虑将学生列表设置为ObservableCollection<>并将其设置为ListViewItemsSource 然后,集合中的更改将反映在您的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 中删除项目 C# 将项目从列表添加到列表视图 如何使用删除按钮从ListView C#中删除多个项目 如何从ListView组件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> 循环删除列表中的多个项目…c# 如何删除列表C#中的一项? 从C#中的ListView中删除项目 如何在ASP.NET C#中删除特定的listview项目? C#WPF循环并删除选中的ListView项目
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM