Remove Row DataGridView Windows Form C #

1

I'm trying to delete row by line in DataGridView , these lines would be in the case files (attachments) without any database connection, only the following error message appears:

  

It is not possible to programmatically remove rows unless DataGridView is associated with data in a% change with support for change notification and allows deletion.

Follow the code below:

private void DataGrid1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 4)
        DataGrid1.Rows.RemoveAt(DataGrid1.CurrentRow.Index);
}
    
asked by anonymous 07.06.2016 / 18:03

1 answer

2

See if it helps!

      private void Form1_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new[]
            {
                new DataColumn("Nome"), new DataColumn("Idade"),
                new DataColumn("Endereco")
            });
        string[] nomes = {"Getulio Vargas", "Juscelino Kubitschek"};
        int[] idade = {134, 114};
        string[] endereco = { "São Borja - Rio Grande Do Sul", "Diamantina - Minas Gerais" };

        for (var i = 0; i < nomes.Count(); i++)
            dt.Rows.Add(nomes[i], idade[i], endereco[i]);
        dataGridView1.DataSource = dt;

    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (dataGridView1.CurrentRow == null) return;
        dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
    }
    
07.06.2016 / 18:45