DataGridView throwing “InvalidOperationException: Operation is not valid…” when adding a row

前端 未结 5 1677
失恋的感觉
失恋的感觉 2021-02-07 05:00

I want an OpenFileDialog to come up when a user clicks on a cell, then display the result in the cell.

It all works, except that the DataGridView displays an extra row,

5条回答
  •  没有蜡笔的小新
    2021-02-07 05:44

    I found a workaround on this page, though I don't know why it works

    public MyForm()
    {
        InitializeComponent();
        //Create a BindingSource, set its DataSource to my list,
        //set the DataGrid's DataSource to the BindindingSource...
        _bindingSource.AddingNew += OnAddingNewToBindingSource;
    }
    
    private void OnAddingNewToBindingSource(object sender, AddingNewEventArgs e)
    {
        if(dataGridView1.Rows.Count == _bindingSource.Count)
        {
            _bindingSource.RemoveAt(_bindingSource.Count - 1);
        }
    }
    

    I'm getting very sick of spending so much time dealing with Visual Studio bugs...

提交回复
热议问题