Problem building data from a DataSet manually C #

1

I have a dataset that contains a structure of 12 columns and I manually add 3 columns Entrada Parámetro , Salida Parámetro and Parámetro from another dataset , then link it to DataGridView , I am working with date range to load the data, but I have been presented with a case that I do not know how to solve. I already have my dataset with the desired columns added, it is enough to insert the data in the corresponding rows, before detailing the information relevant to the question. The problem is the following: .

  

1 - dataset_manual is to which I add the 3 additional columns manually. (You may or may not have data)

     

2 - dataset_datos will always have data loaded from a query.

Several cases may occur with the data:

1- If the dataset_manual does not have data, I will fill it only with the data of dataset_datos (This is solved).

2- If the dataset_manual has the same amount of data as the dataset_datos fill all the columns in their corresponding row. (This is resolved)

3- If the dataset_manual has less data than the dataset_datos and the value of the rows correspond in both dataset with the same date value and the position of the value is in the same index, fill the dataset_manual with the corresponding rows and the 3 additional columns fill them with dataset_datos (This is resolved)

4- if the dataset_manual has less data than the dataset_datos and the value of the rows NO corresponds in both dataset with the same date value and the position of the value is not in the same index. (THIS IS THE PROBLEM) .

I present an example to be able to understand more clearly, suppose the following data for each dataset:

I currently have this method that works for me: 1, 2, 3:

int cantidad = dataset_manual.Tables[0].Rows.Count;

for (int indice = 0; indice < dataset_datos.Tables[0].Rows.Count; indice++)
{
    if (indice < cantidad_validacion)
    {
        //Actualizo las celdas
        dataset_manual.Tables[0].Rows[indice]["entrada_parametro"] = dataset_datos.Tables[0].Rows[indice]["entrada_parametro"];
        dataset_manual.Tables[0].Rows[indice]["salida_parametro"] = dataset_datos.Tables[0].Rows[indice]["salida_parametro"];
        dataset_manual.Tables[0].Rows[indice]["parametro"] = dataset_datos.Tables[0].Rows[indice]["parametro"].ToString();
    }
    else
    {
        //Creo las filas
         var fila = dataset_manual.Tables[0].NewRow();
         fila["entrada_parametro"] = dataset_datos.Tables[0].Rows[indice]["entrada_parametro"];
         fila["salida_parametro"] = dataset_datos.Tables[0].Rows[indice]["salida_parametro"];
         fila["parametro"] = dataset_datos.Tables[0].Rows[indice]["parametro"].ToString();
         dataset_manual.Tables[0].Rows.Add(fila);
     }
}

In this way I get the following:

  

Entry / Exit time: 01:00:00 p.m. and 07:00:00 p.m. correspond to date / date: 16/09/2017 and NO to day 12.

The result I want to get is as follows:

To achieve this I have thought of using two cycles to go through both and ask if the date is the same, but I will be altering the dataset_manual and distorting the data:

                for (int indice = 0; indice < dataset_datos.Tables[0].Rows.Count; indice++)
                {
                    for (int indice2 = 0; indice2 < dataset_manual.Tables[0].Rows.Count; indice2++)
                    {
                        DateTime fecha_dsmanual = new DateTime();
                        DateTime fecha_dsdatos = new DateTime();

                        fecha_dsmanual = Convert.ToDateTime(dataset_manual.Tables[0].Rows[indice2]["fecha"].ToString());

                        fecha_dsdatos = Convert.ToDateTime(dataset_datos.Tables[0].Rows[indice]["fecha"].ToString());

                        if (fecha_dsmanual.ToShortDateString().Equals(fecha_dsdatos.ToShortDateString()))
                        {
                            //Actualizo las celdas
                            dataset_manual.Tables[0].Rows[indice2]["entrada_parametro"] = dataset_datos.Tables[0].Rows[indice]["entrada_parametro"];
                            dataset_manual.Tables[0].Rows[indice2]["salida_parametro"] = dataset_datos.Tables[0].Rows[indice]["salida_parametro"];
                            dataset_manual.Tables[0].Rows[indice2]["parametro"] = dataset_datos.Tables[0].Rows[indice]["parametro"].ToString();

                        }
                        else
                        {
                            //Creo las filas
                            var fila = dataset_manual.Tables[0].NewRow();
                            fila["entrada_parametro"] = dataset_datos.Tables[0].Rows[indice]["entrada_parametro"];
                            fila["salida_parametro"] = dataset_datos.Tables[0].Rows[indice]["salida_parametro"];
                            fila["parametro"] = dataset_datos.Tables[0].Rows[indice]["parametro"].ToString();
                            dataset_manual.Tables[0].Rows.Add(fila);
                        }
                    }
                }

  

Can you suggest an idea?

Environment: Visual Studio 2010 (WindowsForms C #) & .NET NetFramework 4

    
asked by J. Rodríguez 15.02.2018 в 20:55
source

1 answer

1

I think you have a basic problem, and that is that you update the rows based on your index, instead of looking for exactly what row you should update. What I would do is use LINQ to search the row, and update it if it exists and if not create a new one. Something like this:

foreach (DataRow dr in ds.Tables[0].AsEnumerable().OrderBy(x=>x.Field<DateTime>("Fecha")).ToList())
{
    var fila=dataset_manual.Tables[0].AsEnumerable().Where(x=> x.Field<DateTime>("fecha").Date == ((DateTime)dr["fecha"]).Date).FirstOrDefault();

    if (fila!=null)
    {
         //Actualizar datos
         fila["entrada_parametro"]=(DateTime)dr["entrada_parametro"];
         fila["salida_parametro"]=(DateTime)dr["salida_parametro"];
         fila["parametro"]=dr["parametro"].ToString();
    }
    else
    {
         //Añadir fila
         var fila2 = dataset_manual.Tables[0].NewRow();
         fila2["entrada_parametro"] = dr["entrada_parametro"];
         fila2["salida_parametro"] = dr["salida_parametro"];
         fila2["parametro"] = dr["parametro"].ToString();
         dataset_manual.Tables[0].Rows.Add(fila2);
    }
}
    
answered by 16.02.2018 / 09:07
source