1
votes

I have a DataGridView as in another question and AllowUserToDeleteRows is set to true.

The docs say that IBindingList.AllowRemove should also be set to true. However, a List doesn't seem to have that interface, and it doesn't seem to need it. One can remove items from a List.

A similar question's answer suggests setting DataGridView.EditMode to EditOnKeystroke. But that doesn't help.

So - How can I get it to allow a user to Delete Rows?

2

2 Answers

2
votes

I was able to delete it with no problem:

public class MyDataList : List<MyData>
{
    public MyDataList()
    {
        Add(new MyData { ID = 1, Name = "Name 1" });
        Add(new MyData { ID = 2, Name = "Name 2" });
        Add(new MyData { ID = 3, Name = "Name 3" });
        Add(new MyData { ID = 4, Name = "Name 4" });
        Add(new MyData { ID = 5, Name = "Name 5" });
    }
}


public class MyData
{
    public int ID { get; set; }
    public string Name { get; set; }
}

public partial class Form1 : Form
{
    BindingSource myDataListBindingSource;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        myDataListBindingSource = new BindingSource();
        myDataListBindingSource.DataSource = new MyDataList();
        dataGridView1.DataSource = myDataListBindingSource;
    }
}

Result:

enter image description here

0
votes

From what I can see you're using a generic List as a data source to DataGridView. I don't think it is possible to automatically delete rows that way. You could substitute the List with BindingList and do as the docs say, or have a listener for when user tries to delete the row, set DataSource to null, delete the entry from the list and then assign that list to the datasource again (and possibly invalidate to force DataGridView to be redrawn).