Datagridview - remove button

Asked

Viewed 90 times

-1

Good morning. I need help to solve the following problem: I need to do a project in which I insert a code and name of a person to be stored in a datagridview (windows Forms c#), but when I use the following line that was passed to me for help, ends up giving error when I click the same button to remove even if I have no Row or selected cell, I would like help to create a if to prevent or to send a message to the person who presses the button when no cell is selected.

    private void BtnLimparDados_Click(object sender, EventArgs e)
    {
        txtCodigo.Text = "";
        txtNome.Text = "";
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void btnAdicionar_Click(object sender, EventArgs e)
    {
        dgvDados.Rows.Add(txtCodigo.Text, txtNome.Text);
        txtCodigo.Text = "";
        txtNome.Text = "";
        txtNome.Focus();
    }

    private void btnRemover_Click(object sender, EventArgs e)
    {
        int row = dgvDados.CurrentCell.RowIndex;
        if (dgvDados.CurrentCell.RowIndex == -1)
        {
            MessageBox.Show("a", "a", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
        else

            dgvDados.Rows.RemoveAt(row);

    }

    private void dgvDados_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        int row = e.RowIndex;
        dgvDados.Rows.RemoveAt(row);
    }
}

}

1 answer

0

The dataGridView has been implemented in a way that it accounts for the last line (empty), so you should check before if you are not trying to delete it, because that is what is causing error, the code below worked perfectly here, and in addition it allows to exclude multiple selection and line selection. Follows:

private void btnRemover_Click(object sender, EventArgs e)
{
    var totalDeLinhas = dgvDados.RowCount;
    //Use um hashset para não incluir linhas duplicadas (pode usar linq também)
    HashSet<DataGridViewRow> linhasAfetadas = new HashSet<DataGridViewRow>();

    foreach (DataGridViewCell celula in dgvDados.SelectedCells)
        //Conferir se a linha selecionada não é a última
        if (!celula.OwningRow.IsNewRow)
            linhasAfetadas.Add(celula.OwningRow);

    //Se não houver nenhuma linha afetada mostrar a mensagem de erro
    if (linhasAfetadas.Count == 0)
        MessageBox.Show("a", "a", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

    foreach (var linha in linhasAfetadas)
    {
        dgvDados.Rows.Remove(linha);
    }
}

If you don’t need to show error message gets easier:

private void btnRemover_Click(object sender, EventArgs e)
{
    var celulasSelecionadas = dgvDados.SelectedCells;
    foreach (DataGridViewCell celula in celulasSelecionadas)
    {
        if (celula.RowIndex != -1)
            if (!celula.OwningRow.IsNewRow)
                dgvDados.Rows.Remove(celula.OwningRow);
    }
}

Or if you prefer to use:

private void btnRemover_Click(object sender, EventArgs e)
{
    dgvDados.SelectedCells
        .OfType<DataGridViewCell>()
        .GroupBy(celula => celula.OwningRow)
        .Select(linha => linha.Key)
        .Where(linha => !linha.IsNewRow)
        .ToList<DataGridViewRow>()
        .ForEach(linha => dgvDados.Rows.Remove(linha));
}

Browser other questions tagged

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