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.
and07: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