我知道標(biāo)題太長(zhǎng),我會(huì)解釋一下情況:我將數(shù)據(jù)網(wǎng)格視圖綁定到列表:BindingList<MyViewModel> _list = new BindingList<MyViewModel>{ new MyViewModel{ Num =1 , Val ="test"}, new MyViewModel{ Num =2 , Val ="test1"}, new MyViewModel{ Num =3 , Val ="test2"},};dataGridView1.DataSource = _list;dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;dataGridView1.MultiSelect = false;我綁定了事件來(lái)監(jiān)聽(tīng) Delete 命令: dataGridView1.KeyDown += (s, ev) => { if (ev.KeyCode == Keys.Delete) { var item = dataGridView1.SelectedRows[0].DataBoundItem as MyViewModel; if (item != null) { _list.Remove(item); dataGridView1.Rows[0].Selected = true; } } };和視圖模型看起來(lái)像:public sealed class MyViewModel{ public string Val { get; set; } public int Num { get; set; }}在網(wǎng)格中我將有 3 個(gè)項(xiàng)目:如果我刪除項(xiàng)目 3,項(xiàng)目 2 也會(huì)被刪除(或者可能隱藏)為什么 ?刪除一個(gè)項(xiàng)目后,我想從數(shù)據(jù)網(wǎng)格中選擇第一個(gè)項(xiàng)目dataGridView1.Rows[0].Selected = true;。如果我刪除dataGridView1.Rows[0].Selected = true;則很好,項(xiàng)目 2 是可見(jiàn)的。
從綁定列表中刪除一個(gè)元素并在 DataGridView 中選擇一行后,該行也會(huì)在 Winforms
繁華開(kāi)滿天機(jī)
2022-12-31 10:52:44