为什么在删除 DataGridView 控件中的行时出现此错误? [英] Why I'm getting this error when deleting a row in DataGridView control?

查看:23
本文介绍了为什么在删除 DataGridView 控件中的行时出现此错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在删除 DataGridView 控件中的一行时出现此错误?我该如何解决这个问题?

Why I'm getting this error when deleting a row in DataGridView control? How can I address this issue?

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 没有实现 IBindingList,

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

你需要使用一个实现了IBindingList

使用 BindingListDataTable 代替

这篇关于为什么在删除 DataGridView 控件中的行时出现此错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆