为什么在删除 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);
}
}
List<T>
不实施IBindingList
,
public class List<T> : IList<T>, ICollection<T>,
IEnumerable<T>, IList, ICollection, IEnumerable
您需要使用一个实现的类IBindingList
Use a BindingList<T> http://msdn.microsoft.com/en-us/library/ms132679.aspx or DataTable http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx instead
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)