Remove Datagridview line Windows Form C#

Asked

Viewed 1,762 times

1

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

It is not possible to programmatically remove lines unless DataGridView is associated with data in a IBindingList with support for change notification and allowing exclusion.

Follows code below:

private void DataGrid1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 4)
        DataGrid1.Rows.RemoveAt(DataGrid1.CurrentRow.Index);
}
  • You want to remove the lines that are selected by you?

  • That, I want to remove the lines I select. There are already some functions. "Open" that downloads, "Comments", and to deploy the "Delete". @Fábioarsenic

  • You can put the code where you "feed" this Datagrid?

  • @jbueno There are 2 btn with click event, one that attaches the file and another that saves the file in Datagrid, would be these?

  • The code that puts the data on the grid, whatever it is.

  • @Fábioarsenic unsuccessful attempt.

  • @jbueno unfortunately not. Only this information I can provide. Sorry.

  • No need to apologize. I will have to vote to close the question as is not clear enough, because we can’t help without more information. You can [Edit] your post at any time to try to explain your problem better.

Show 3 more comments

1 answer

2

See if it helps you!

      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);
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.