簡體   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