//ESTE METODO SE ENCARGA DE IMPACTAR LOS NUEVOS CAMBIOS A LA BASE DE DATOS public void ActualizaTabla(string Tabla, string campoClave, bool claveSecuencia) { // Obtener de DetallesColumnasTablas todos los campos de la tabla menos el campo clave // Pasar esos campos a un arreglo de campos y valores // actualizar bool datosOK = true; Tablas otabla = new Tablas(); TablasBus tablasGrd = new TablasBus(); otabla = tablasGrd.TablasGetById(Tabla); if (ListDetalle == null) { DetallesColumnasTablasBus oDetalleGrd = new DetallesColumnasTablasBus(); ListDetalle = oDetalleGrd.DetallesColumnasTablasGetByCodigo(Tabla); } //ACA SE RECORREN TODAS LAS ROWS DE LA GRILLA foreach (DataGridViewRow row in _vista.grilla.Rows) { if (!row.IsNewRow) { string caso = row.Cells["ALTERADO"].Value.ToString(); if (caso != "0") { string[] nombreCampos = { }; string[] valoresCampos = { }; string valorClave = ""; string columnaClave = ""; int posicion = 0; // Actualizar tabla foreach (DetallesColumnasTablas oDetalle in ListDetalle) { posicion++; if (oDetalle.DctDescripcion != campoClave || (row.Cells[_vista.grilla.ColumnCount - 1].Value.ToString() == "3" && oDetalle.DctDescripcion == campoClave && !claveSecuencia)) { Array.Resize(ref nombreCampos, nombreCampos.Length + 1); Array.Resize(ref valoresCampos, valoresCampos.Length + 1); nombreCampos[nombreCampos.Length - 1] = oDetalle.DctColumna; if (row.Cells[posicion - 1].Visible) { if (row.Cells[posicion - 1].ValueType == typeof(DateTime)) { String fechatmp; fechatmp = row.Cells[posicion - 1].FormattedValue.ToString(); valoresCampos[valoresCampos.Length - 1] = fechatmp; } else { if (row.Cells[posicion - 1].Value != null) { valoresCampos[valoresCampos.Length - 1] = row.Cells[posicion - 1].Value.ToString(); } else { if (oDetalle.DctTipoControl == "CHKCK") { valoresCampos[valoresCampos.Length - 1] = ((DataGridViewCheckBoxCell)row.Cells[posicion - 1]).FalseValue.ToString(); } } } } else // Si la columna a actualizar no es visible tiene una homonima visible // Busco la homonima visible y tomo su valor que es el que debo tener en cuenta para actualizar //ACA TENDRIA QUE RECUPERAR EL VALOR DEL cmbLista, pero no lo hace!!!!!!!!!!!! { object celda = row.Cells[oDetalle.DctColumna]; switch (oDetalle.DctTipoControl) { case "COMBO": { valoresCampos[valoresCampos.Length - 1] = ((DataGridViewComboBoxCell)celda).Value.ToString(); break; } case "CHKCK": { valoresCampos[valoresCampos.Length - 1] = ((DataGridViewCheckBoxCell)celda).Value.ToString(); break; } case "FECHA": { valoresCampos[valoresCampos.Length - 1] = ((DataGridViewTextBoxCell)celda).Value.ToString(); valoresCampos[valoresCampos.Length - 1] = valoresCampos[valoresCampos.Length - 1].Substring(0, 9); break; } } } } else { if (row.Cells[posicion - 1].Value != null) { valorClave = row.Cells[posicion - 1].Value.ToString(); } columnaClave = oDetalle.DctColumna; } } ; //FIN FOREACH DE DETALLES COLUMNA // Para cada registro deberia actualizar su estado si se pudo actualizar switch (caso) { case "1": { // Update // Para cada campo validar que tenga el tipo de dato adecuado // contra la estructura de la tabla que esta en _estructuraTablaGrilla datatable // Si todos los campos menos la clave son validos actualizo foreach (DataRow oCampo in _estructuraTablaGrilla.Rows) { int pos; pos = Array.IndexOf(nombreCampos, oCampo.ItemArray[2]); if (pos >= 0 && datosOK) { datosOK = (oUtil.TipoDatoValido(valoresCampos[pos], oCampo.ItemArray[4].ToString())); } //MessageBox.Show(oCampo.ItemArray[2].ToString()); // campo } if (datosOK && tablasGrd.TablaActualizaGrid(otabla.TabNombre, nombreCampos, valoresCampos, columnaClave + "='" + valorClave + "'", "U")) { row.Cells[_vista.grilla.ColumnCount - 1].Value = "0"; } break; } case "2": { // Delete if (datosOK && tablasGrd.TablaActualizaGrid(otabla.TabNombre, nombreCampos, valoresCampos, columnaClave + "='" + valorClave + "'", "D")) { _vista.grilla.Rows.Remove(row); row.Cells[_vista.grilla.ColumnCount - 1].Value = "0"; } break; } case "3": { // Insert // Para cada campo validar que tenga el tipo de dato adecuado // contra la estructura de la tabla que esta en _estructuraTablaGrilla datatable // Si todos los campos inclusive la clave, si no es secuencia, son validos actualizo foreach (DataRow oCampo in _estructuraTablaGrilla.Rows) { int pos; pos = Array.IndexOf(nombreCampos, oCampo.ItemArray[2]); if (pos >= 0 && datosOK) { datosOK = (oUtil.TipoDatoValido(valoresCampos[pos], oCampo.ItemArray[4].ToString())); if (valoresCampos[pos] != null) { datosOK = valoresCampos[pos].Length > 0 || oCampo.ItemArray[8].ToString() == "Y"; } } } if (claveSecuencia) { if (datosOK && tablasGrd.TablaActualizaGrid(otabla.TabNombre, nombreCampos, valoresCampos, columnaClave + "='" + valorClave + "'", "IN")) { row.Cells[_vista.grilla.ColumnCount - 1].Value = "0"; } } else { if (datosOK && tablasGrd.TablaActualizaGrid(otabla.TabNombre, nombreCampos, valoresCampos, columnaClave + "='" + valorClave + "'", "I")) { row.Cells[_vista.grilla.ColumnCount - 1].Value = "0"; } } break; } } } ; if (!datosOK) { MessageBox.Show("Error al actualizar los datos, Revice las filas resaltadas."); } } } }
public void ActualizaTabla(string Tabla, string campoClave, bool claveSecuencia) { // Obtener de DetallesColumnasTablas todos los campos de la tabla menos el campo clave // Pasar esos campos a un arreglo de campos y valores // actualizar bool datosOK = true; Tablas otabla = new Tablas(); TablasBus tablasGrd = new TablasBus(); otabla = tablasGrd.TablasGetById(Tabla); DetallesColumnasTablasBus oDetalleGrd = new DetallesColumnasTablasBus(); List <DetallesColumnasTablas> ListDetalle = oDetalleGrd.DetallesColumnasTablasGetByCodigo(Tabla); foreach (DataGridViewRow row in _vista.grilla.Rows) { if (!row.IsNewRow) { string[] nombreCampos = { }; string[] valoresCampos = { }; string valorClave = ""; string columnaClave = ""; int posicion = 0; // Actualizar tabla foreach (DetallesColumnasTablas oDetalle in ListDetalle) { posicion++; if (oDetalle.DctDescripcion != campoClave || (row.Cells[_vista.grilla.ColumnCount - 1].Value.ToString() == "3" && oDetalle.DctDescripcion == campoClave && !claveSecuencia)) { Array.Resize(ref nombreCampos, nombreCampos.Length + 1); Array.Resize(ref valoresCampos, valoresCampos.Length + 1); nombreCampos[nombreCampos.Length - 1] = oDetalle.DctColumna; if (row.Cells[posicion - 1].Visible) { if (row.Cells[posicion - 1].ValueType == typeof(DateTime)) { String fechatmp; fechatmp = row.Cells[posicion - 1].FormattedValue.ToString(); valoresCampos[valoresCampos.Length - 1] = fechatmp; } else { valoresCampos[valoresCampos.Length - 1] = row.Cells[posicion - 1].Value.ToString(); } } else // Si la columna a actualizar no es visible tiene una homonima visible // Busco la homonima visible y tomo su valor que es el que debo tener en cuenta para actualizar { for (int pos = posicion; pos < row.Cells.Count; pos++) { if (_vista.grilla.Columns[posicion - 1].Name == _vista.grilla.Columns[pos].Name) { if (_vista.grilla.Columns[pos].ValueType == typeof(string)) { valoresCampos[valoresCampos.Length - 1] = row.Cells[pos].Value.ToString(); } else //Es un checkbox { valoresCampos[valoresCampos.Length - 1] = row.Cells[pos].Value.Equals(true) ? "S" : "N"; } } } } } else //Agrego un registro y la clave no es secuencia { valorClave = row.Cells[posicion - 1].Value.ToString(); columnaClave = oDetalle.DctColumna; } } ; // Para cada registro deberia actualizar su estado si se pudo actualizar if (row.Cells[_vista.grilla.ColumnCount - 1].Value.ToString() == "1" && !row.IsNewRow) { // Update // Para cada campo validar que tenga el tipo de dato adecuado // contra la estructura de la tabla que esta en _estructuraTablaGrilla datatable // Si todos los campos menos la clave son validos actualizo foreach (DataRow oCampo in _estructuraTablaGrilla.Rows) { int pos; pos = Array.IndexOf(nombreCampos, oCampo.ItemArray[2]); if (pos >= 0 && datosOK) { datosOK = (oUtil.TipoDatoValido(valoresCampos[pos], oCampo.ItemArray[4].ToString())); } //MessageBox.Show(oCampo.ItemArray[2].ToString()); // campo } if (datosOK && tablasGrd.TablaActualizaGrid(otabla.TabNombre, nombreCampos, valoresCampos, columnaClave + "='" + valorClave + "'", "U")) { row.Cells[_vista.grilla.ColumnCount - 1].Value = "0"; } } else if (row.Cells[_vista.grilla.ColumnCount - 1].Value.ToString() == "2" && !row.IsNewRow) { // Delete tablasGrd.TablaActualizaGrid(otabla.TabNombre, nombreCampos, valoresCampos, columnaClave + "='" + valorClave + "'", "D"); } else if (row.Cells[_vista.grilla.ColumnCount - 1].Value.ToString() == "3" && !row.IsNewRow) { // Insert // Para cada campo validar que tenga el tipo de dato adecuado // contra la estructura de la tabla que esta en _estructuraTablaGrilla datatable // Si todos los campos inclusive la clave, si no es secuencia, son validos actualizo foreach (DataRow oCampo in _estructuraTablaGrilla.Rows) { int pos; pos = Array.IndexOf(nombreCampos, oCampo.ItemArray[2]); if (pos >= 0 && datosOK) { datosOK = (oUtil.TipoDatoValido(valoresCampos[pos], oCampo.ItemArray[4].ToString())); datosOK = valoresCampos[pos].Length > 0 || oCampo.ItemArray[8].ToString() == "Y"; } } if (datosOK && tablasGrd.TablaActualizaGrid(otabla.TabNombre, nombreCampos, valoresCampos, columnaClave + "='" + valorClave + "'", "I")) { row.Cells[_vista.grilla.ColumnCount - 1].Value = "0"; } } ; } } ; if (!datosOK) { MessageBox.Show("Error al actualizar los datos, Revice las filas resaltadas."); } }