Detect changes made in WindowsForms controls, C #

5

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 called HasChanges 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 function Verificar_cambios I performed the validation if the value found in dataset original is the same as found in the otro dataset if it is the same the botón Salvar is not enabled, otherwise the botó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

    
asked by J. Rodríguez 05.01.2018 в 22:50
source

1 answer

1

It would be much easier if you work with collections of objects and Linq, even better with entity framework and changetracker, but for the example you have passed this is a solution:

Add AcceptChanges in the method generate_data (the changes will be obtained from this moment)

dataset_datos.AcceptChanges();

myDataGrid.DataSource = dataset_datos.Tables[0];

To get the changes in the verify_change method

  if (dataset_datos.HasChanges())
  {
     var tempDataSet = dataset_datos.Tables[0].GetChanges(DataRowState.Modified);
     var tempDataSet1 = dataset_datos.GetChanges(DataRowState.Added);
     var tempDataSet2 = dataset_datos.GetChanges(DataRowState.Deleted);
     var tempDataSet3 = dataset_datos.GetChanges(DataRowState.Unchanged);  
  }

So we recover the current values and the original ones

DataRow[] ModifiedCurrentRows = dataset_datos.Tables[0].Select(null, null, DataViewRowState.ModifiedCurrent);

foreach (DataRow Row in ModifiedCurrentRows)
{
        foreach (DataColumn catCol in dataset_datos.Tables[0].Columns)
        {
                 Console.Write(Row[catCol, DataRowVersion.Original] + "\t");
                 Console.Write(Row[catCol, DataRowVersion.Current] + "\t");

        }
         Console.WriteLine();
}
    
answered by 07.01.2018 / 13:16
source