I need to detect the changes that were made to the form, either in TextBox
, Datagridview
or other controls.
Here is a small example as a test code (GoogleDrive Link )
IdentificarCambios.zip
This works Perfectly for me , but I think there should be a more optimal and practical method. I know that the
Dataset
contains a method calledHasChanges
but I do not know how to use it to get the result I want.
Here I begin the explanation:
Here we see this form as you can see the Boton Salvar
is disabled, because they are the original data loaded initially. If I make any change in any of these cells: Hora Entrada
, Hora Salida
, Cantidad
after the event CellEndEdit
is triggered, we invoke a Función
called verificar_cambios
which works as follows:
Initially when the data is loaded I have two
dataset
of which the original dataset copied it to the second dataset so that they contain the same data, then in the functionVerificar_cambios
I performed the validation if the value found indataset original
is the same as found in theotro dataset
if it is the same thebotón Salvar
is not enabled, otherwise thebotón Salvar
is enabled.
As you can see in the second image, I changed the value of the celda
of 5
to 10
the function was invoked and enabled the botón salvar
. But if the usuario
returns its value to 5
the function again deshabilita
the botón salvar
. I do not know how to achieve that with HasChanges
.
EDITED
Fragment of the Code:
When you upload the data:
dataset_cambios = dataset_datos.Copy();
Check Changes feature:
private void verificar_cambios()
{
string cantidad = string.Empty cantidad_cambios = string.Empty;
for (int fila = 0; fila < dataset_datos.Tables[0].Rows.Count; fila++)
{
if (dataset_datos.Tables[0].Rows[fila]["cantidad"].ToString() == string.Empty)
cantidad = string.Empty;
else
cantidad = dataset_datos.Tables[0].Rows[fila]["cantidad"].ToString().Trim();
if (dataset_cambios.Tables[0].Rows[fila]["cantidad"].ToString() == string.Empty)
cantidad_cambios = string.Empty;
else
cantidad_cambios = dataset_cambios.Tables[0].Rows[fila]["cantidad"].ToString().Trim();
//Verificando si la variable del dataset original es diferente a la del dataset guardado inicialmente.
if (cantidad.Equals(cantidad_cambios))
{
//No hay cambios
btn_salvar.Enabled = false;
}
else
{
//Si hay cambios
btn_salvar.Enabled = true;
break;
}
}
}
So, How do I optimize this method, or create a more efficient one?
Note: I have not placed all the code of the project so as not to enlarge the publication so much.
Visual Studio 2010 and .NET Framework 4