Tengo una función que no me permite ingresar datos iguales en una misma fila. al momento de ingresar un nuevo producto si funciona no puedo ingresar un producto que ya esta en la base de datos,el problema si yo quiero modificar y solo quiero cambiar ejemplo el precio de venta no se puede porque no se puede guardar con el mismo nombre ALGUNA IDEA ?
ESTE ES MI BOTON GUARDAR
private void btnGuardar_Click(object sender, EventArgs e)
{
try
{
if (modificar)
{
Eproductos editar = new Eproductos();
bool exist = dgvfiltrarproductos.Rows.Cast<DataGridViewRow>().Any(row => Convert.ToString(row.Cells["Producto"].Value) == txtnombre.Text);
bool exist2 = dgvfiltrarproductos.Rows.Cast<DataGridViewRow>().Any(row => Convert.ToString(row.Cells["CodSAC"].Value) == txtCodSAC.Text);
//SI EL VALOR NO EXISTE EN LA BASE DE DATOS LO INGRESA
if (!exist == true)
{
if (!exist2 == true)
{
editar.Codproducto = Convert.ToInt32(txtnombre.Tag.ToString());
editar.Producto = txtnombre.Text;
editar.CodSac = txtCodSAC.Text;
editar.Marca.Codmarca = Convert.ToInt32(txtmarca.Text.ToString());
editar.Existencia = Convert.ToDecimal(txtexistencia.Text.ToString());
editar.Precio = Convert.ToDecimal(txtprecioventa.Text.ToString());
editar.Costo = Convert.ToDecimal(txtpreciocompra.Text.ToString());
if (rbtnActvivo.Checked == true)
{
editar.Activo = true;
}
else
{
editar.Activo = false;
}
Nproductos gestion = new Nproductos();
gestion.modificar(editar);
MessageBox.Show("Se modifico correctamente", "PRODUCTO", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Este CodSAC ya fue ingresado", "REVISE");
}
}
//SI EL VALOR EXISTE EN LA BASE DE DATOS NO LO INGRESA
else
{
MessageBox.Show("Este producto ya fue ingresado", "REVISE");
}
}
else
{
Eproductos nuevo = new Eproductos();
//COMPROBAR SI EL REGISTRO YA EXISTE CON UNA CONSULTA "LINQ" RECORRE TODA LA FILA DEL DATAGRID VIEW Y LA COLUMNA ESPECIFICA .
bool exist = dgvfiltrarproductos.Rows.Cast<DataGridViewRow>().Any(row => Convert.ToString(row.Cells["Producto"].Value) == txtnombre.Text);
bool exist2 = dgvfiltrarproductos.Rows.Cast<DataGridViewRow>().Any(row => Convert.ToString(row.Cells["CodSAC"].Value) == txtCodSAC.Text);
//SI EL VALOR NO EXISTE EN LA BASE DE DATOS LO INGRESA
if (!exist == true)
{
if (!exist2 == true)
{
nuevo.Producto = txtnombre.Text;
nuevo.Marca.Codmarca = Convert.ToInt32(txtmarca.Text.ToString());
nuevo.CodSac = txtCodSAC.Text;
nuevo.Existencia = Convert.ToDecimal(txtexistencia.Text.ToString());
nuevo.Precio = Convert.ToDecimal(txtprecioventa.Text.ToString());
nuevo.Costo = Convert.ToDecimal(txtpreciocompra.Text.ToString());
if (rbtnActvivo.Checked == true)
{
nuevo.Activo = true;
}
else
{
nuevo.Activo = false;
}
Nproductos gestion = new Nproductos();
gestion.agregar(nuevo);
MessageBox.Show("Se guardo correctamente", "PRODUCTO", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
listaproducto.Add(nuevo);
MessageBox.Show("Este CodSAC ya fue ingresado", "REVISE");
}
}
//SI EL VALOR EXISTE EN LA BASE DE DATOS NO LO INGRESA
else
{
listaproducto.Add(nuevo);
MessageBox.Show("Este producto ya fue ingresado", "REVISE");
}
}
ActualizarLista();
Limpiar();
Deshabilitar();
btnGuardar.Enabled = false;
btnCancelar.Enabled = false;
btnModificar.Enabled = false;
btnBuscarmarca.Enabled = false;
btnNuevo.Enabled = true;
modificar = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}