作者kevin8685 (最愛maki~!)
看板C_Sharp
標題[問題] 問一個listbox的刪除問題@@
時間Sun Jun 13 22:02:29 2010
我想要寫個商品管理的系統
這是我寫在form的一段
SortedList product = new SortedList();
StreamReader item = new StreamReader("product.txt");
while (!item.EndOfStream)
{
string[] item2 = item.ReadLine().Split(' ');
product.Add(item2[0], item2[1] + "\t" + item2[2]);
BindingSource bs = new BindingSource();
bs.DataSource = product;
listBox1.DataSource = bs;
listBox1.DisplayMember = "Value";
listBox1.ValueMember = "Key";
listBox1.ClearSelected();
}
之後我想要
在按鈕寫一個"刪除選取的項目"
可是一直出現
"已設定 DataSource 屬性時,無法修改項目集合。"
這是我按鈕的寫法
while (listBox1.SelectedItems.Count != 0)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndices[0]);
}
請問怎麼修改@@
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.117.14.212
→ andymai:找到答案了嗎?其實那個錯誤從另一個角度來看也算是告知該 06/14 18:57
→ andymai:怎麼做了~不能在設定後移除~那就把listBox1.DataSource拿 06/14 18:58
→ andymai:出來轉型之後再移除就好了~也不必再設回DataSource... 06/14 18:59