2011-12-06 13 views
8

なぜDataGridViewコントロールの行を削除するときにこのエラーが発生しますか? この問題にはどのように対処できますか?DataGridViewコントロールで行を削除すると、なぜこのエラーが発生しますか?

Rows cannot be programmatically removed unless the DataGridView is data-bound to an IBindingList that supports change notification and allows deletion. 

public partial class Form1 : Form 
    { 
     List<Person> person = new List<Person>(); 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     void Form1Load(object sender, EventArgs e) 
     { 
      person.Add(new Person("McDonalds", "Ronald")); 
      person.Add(new Person("Rogers", "Kenny"));   
      dataGridView1.DataSource = person; 
     } 

     void BtnDeleteClick(object sender, EventArgs e) 
     { 
      dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index); 
     } 
    } 

答えて

14

List<T>あなたはIBindingList

BindingList<T>またはDataTable代わり

を使用してくださいを実装するクラスを使用する必要が IBindingList

public class List<T> : IList<T>, ICollection<T>, 
    IEnumerable<T>, IList, ICollection, IEnumerable 

を実装していません。

+0

ので、代わりのリストが、私はそれをするBindingList 何かをしなければならないのですか? – yonan2236

+0

はい。それはうまくいくはずです。 –

+0

ありがとうございました。ちょうどこのエラーに直面した:) – Latheesan

2

personリストから要素を削除する必要があります。

person.RemoveAt(0); 
0

私のソリューション:

void BtnDeleteClick(object sender, EventArgs e) 
{ 
    person.RemoveAt(dataGridView1.SelectedRows[0].Index); 
} 
関連する問題