Button Remove c # form button

0

Hi, I want to make the selected rows be eliminated so you look at the interface

This is the code I have but it is not

 private void BtnQuitar_Click(object sender, EventArgs e)
    {
        if (DataGridViewSelectionArea.SelectedRows == null)
        {
            MessageBox.Show("Debes seleccionar una fila del grid");
            return;
        }
        var dt = (DataTable)DataGridViewSelectionArea.DataSource;
        foreach (DataGridViewRow item in DataGridViewSelectionArea.SelectedRows)
        {
            var row = dt.AsEnumerable().FirstOrDefault(r => r["Id"] == item.Cells["Id"].Value);
            if (row != null)
            {
                dt.Rows.Remove(row);
            }
            DataGridViewSelectionArea.DataSource = dt;
        }

But it does not fulfill its function, at the moment of using the button it sends me the messagebox of "you must select a row" and when I try to select the checkbox it does not let me do it, it remains as it is impossible to palom it, I just want to remove the row of selected checkboxes when clicking the remove button

This is the DataGridViewSelectionArea code:

  this.DataGridViewSelectionArea.dataGridView.Location = new System.Drawing.Point(550, 250);
    this.DataGridViewSelectionArea.dataGridView.Name = "DataGridViewSelectionArea";
    this.DataGridViewSelectionArea.dataGridView.Size = new System.Drawing.Size(250, 230);

    this.Controls.Add(this.DataGridViewSectionArea.dataGridView);

Every time I want to remove a row, it launches the MessageBox of "You must select a row of the grid" and I want the selected checkbox rows to be deleted from beforehand, thanks.

    
asked by Alejandro Olivares 02.01.2019 в 23:59
source

1 answer

1

You are making the assignment of the DataTable dt within the same foreach loop where you modify it, it is normal that this causes strange behaviors.

Take this line out of the foreach and see if that works for you:

DataGridViewSelectionArea.DataSource = dt;
    
answered by 03.01.2019 / 09:58
source