Delete rows from a datagridview

0

Hello I try to delete rows with the following code but I get an error. I have tried everything but I can not find out why it will not let me erase the rows of the data gridview.

private void button_BorrarUv_Click(object sender, EventArgs e)
{
    if (dataGridViewUvCnf.CurrentRow.Index != -1)
    {
         dataGridViewUvCnf.Rows.RemoveAt(dataGridViewUvCnf.CurrentRow.Index);
    }
}

I get the following error when I run this:

  

InvalidOperationException: Can not delete rows programmatically unless DataGridView is bound to data with IBindingList that supports change notification and allows deletion.

The Desginer code is as follows

this.button_BorrarUv.Click += new System.EventHandler(this.button_BorrarUv_Click);

Why does this error occur? I have tried to change the EventArgs by DataGridViewCellEventArgs but it does not let me change it I get an error in the designer. I can not know what an IBiningList means. In the properties of the datagridview I have enabled the editing and enable delete rows but it still does not work.

Thanks

    
asked by Xim123 20.02.2018 в 09:21
source

1 answer

2

The DataGridView has basically two modes of operation:

  • One in which you add the information by adding rows to the collection Rows and filling this with data.

In this case the modifications are made directly in the cells, and you add and delete rows directly from the collection Rows

  • Another one in which the information is taken from an external data source linked to the control through the DataSource property.

In this case the modifications should be made to the external data source. In other words, if you want to delete a record, you should remove it from the data source and indicate to DataGridView to refresh the information from the data source.

The error that appears to you indicates that you are using DataGridView to display information from an external data source and that therefore you can not delete rows directly from the Rows collection.

    
answered by 20.02.2018 / 09:56
source