How do I delete a Row from a DataGridView?

0

I comment on my problem, I have a DataGridView where I enter items to make a sale, this is done by typing the code in the first column and using the CellValidating method control that the product exists in a database, that is, in total I have 7 more columns where in the majority I make restrictions like this, either control stock, validate discounts to products, etc.

My problem is that if by chance or inadvertently the user enters the column 'item code' I must skip this validation, solve it by creating a code that is skipped (a zero in my case). The problem is that when I try to delete this row it marks me an error, and I must eliminate it since it interferes with sums that I do to the price column for example.

How could I solve this without generating errors? How can I schedule the row to be deleted when I enter this code?

I accept suggestions and thank you in advance.

This is how the 'item code' column is valid

if (e.FormattedValue.ToString() != "" && (e.FormattedValue.ToString().Length >= 6 && e.FormattedValue.ToString().Length <= 11))
{
      //Valido que no tenga items repetidos
      if (Obj_Vta.ItemRepetido(e.FormattedValue.ToString(), this.dtVentas))
      {
           this.dtVentas.Rows[e.RowIndex].ErrorText = "No se Permiten Items Repetidos - Aumente Cantidad";
           e.Cancel = true;
      }
      else
      {//CONSULTO DE MANERA NORMA
          if (!this.Obj_Vta.BuscarItem(this.dtVentas, e,this.lblCod_TipCte.Text))
          {
              //SI EL ITEM NO EXISTE CANCELAMOS EL AVANCE
              e.Cancel = true;
          }
      }
}
else
{//EN CASO QUE LA CELDA ESTE VACIO O NO CUMPLA CON EL LARGO ADECUADO TENEMOS DOS POSIBILIDADES

    //SI EL VALOR ES 0: SE CANCELA LA OPERACION Y SE DEBERIA ELIMINAR ESA FILA
    if (e.FormattedValue.ToString() == "0")
    {
        e.Cancel = false;
    }
    else
    {
    //EN CASO QUE LA NO QUIERA ELIMINAR LA FILA: MENSAJE DE INGRESE CODIGO CORRECTO O 0 SI QUIERE ABANDONAR
        e.Cancel = true;
        this.dtVentas.Rows[e.RowIndex].ErrorText = "¡¡Ingrese un Codigo!! - 0 si quiere Abandonar";
    }
}

And that's how I try to eliminate the row:

private void dtVentas_CellValidated(object sender, DataGridViewCellEventArgs e)
{
    //METODO QUE SE EJECUTA CUANDO LA CELDA YA ESTA VALIDADA
    if (this.dtVentas.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "0")
    {
        this.dtVentas.Rows.RemoveAt(e.RowIndex);
    }
}
    
asked by Benjamin Garrido H. 20.04.2018 в 17:57
source

1 answer

1

I made a similar practice with datagridview c # Well I'll share the code ...

    // cabe mencionar que este codigo esta en un boton, evento click selecciono el grid a eliminar,
// tiene que seleccionar un elemento del grid.
        if (dgvCaptura.SelectedRows.Count > 0)
        {
           // utilizo el foreach para iterar los registros.
           // con el metodo de RemoveAt. le mando el indice del elemento a eliminar
           foreach (DataGridViewRow item in this.dgvCaptura.SelectedRows)
           {
             dgvCaptura.Rows.RemoveAt(item.Index);                    
           }
        }

I hope I help you.

    
answered by 20.04.2018 в 20:51