我正在使用BindingList作爲我的ListBox的數據源。從ListBox中刪除項目時C#程序崩潰
public static BindingList<memo> memosList = new BindingList<memo>();
每當我嘗試刪除選定的對象(通過按鈕),我的程序崩潰。
private void editMemo_Click(object sender, EventArgs e)
{
listBox1.Items.Remove(listBox1.SelectedItem);
}
我得到以下錯誤:
An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll
Additional information: Items collection cannot be modified when the DataSource property is set.
我也曾嘗試使用:
private void editMemo_Click(object sender, EventArgs e)
{
Form2.memosList.Remove(listBox1.SelectedIndex);
}
但是這不會讓我編譯。
如何在不拋出異常的情況下移除物品?
你想將數據讀入一個列表,然後有一個綁定到列表框中該名單?現在你正試圖從列表框(和數據源)中刪除。第二個是 – Mathemats
,如果你指定一個索引,你想'RemoveAt',我想。 –