DataTable from other DataTable

0

Good evening everyone, I have the following conflict, I am creating two DataTable in C# to go through the content of two tables that are filled by means of a BD , one shows me the code of the product and its name , and another product volumes as well as its code , I need to make a match of both tables to show in a grid the name of the product and the volumes of each one , and I pretend in a third DataTable Result and store the value of the previous two, the table should look like this:

I enclose the code that I am elaborating if someone knows how to do it, I will be very grateful.

private void ValidarInfoGrid()
{
    try
    {
        /*Objetos*/
        DataTable dtProductos = new DataTable();
        DataTable dtStock = new DataTable();
        DataTable dtResultado = new DataTable();
        /*Variables*/
        int prod1 = 0;
        int prod2 = 0;
        int prod3 = 0;
        int prod4 = 0;
        int prod5 = 0;
        int prod6 = 0;
        int codigo = 0;
        int vol1 = 0;
        int vol2 = 0;
        int vol3 = 0;
        int vol4 = 0;
        int vol5 = 0;
        int vol6 = 0;
        string producto = "";
        /*Validaciones*/
        if (cmbEstaciones!=null)
        {
            int estacion = 0;
            estacion = (int)cmbEstaciones.SelectedValue;

            if (estacion > 0)
            {
                dtStock = objVeederRoot.dtVolumenes(estacion);
                if (dtStock.Rows.Count>0)
                {
                    foreach (DataRow drStock in dtStock.Rows)
                    {
                        prod1 = Convert.ToInt32(drStock[0]);
                        vol1 = Convert.ToInt32(drStock[1]);
                        prod2 = Convert.ToInt32(drStock[2]);
                        vol2 = Convert.ToInt32(drStock[3]);
                        prod3 = Convert.ToInt32(drStock[4]);
                        vol3 = Convert.ToInt32(drStock[5]);
                        prod4 = Convert.ToInt32(drStock[6]);
                        vol4 = Convert.ToInt32(drStock[7]);
                        prod5 = Convert.ToInt32(drStock[8]);
                        vol5 = Convert.ToInt32(drStock[9]);
                        prod6 = Convert.ToInt32(drStock[10]);
                        vol6 = Convert.ToInt32(drStock[11]);
                        dtProductos = objVeederRoot.MuestraProductos();
                        foreach (DataRow drProductos in dtProductos.Rows)
                        {
                            codigo = Convert.ToInt32(drProductos[0]);
                            if (prod1 == codigo)
                            {
                                producto = drProductos[1].ToString();
                            }
                            if (prod2 == codigo)
                            {
                                producto = drProductos[1].ToString();
                            }
                            if (prod3 == codigo)
                            {
                                producto = drProductos[1].ToString();
                            }
                            if (prod4 == codigo)
                            {
                                producto = drProductos[1].ToString();
                            }
                            if (prod5 == codigo)
                            {
                                producto = drProductos[1].ToString();
                            }
                            if (prod6 == codigo)
                            {
                                producto = drProductos[1].ToString();
                            }
                            else
                            {
                                producto = "No Aplica";
                            }
                        }
                    }
                    objValidacion.MostrarAviso("Se cargaron los datos de forma Correcta", false, lblAviso);
                }
                else
                {
                    objValidacion.MostrarAviso("No se encontraron registros, intentelo nuevamente", true, lblAviso);
                }

            }
        }
    }
    catch (Exception ex)
    {
        ex.ToString();
    }
}
    
asked by Alberto Arenas 05.01.2017 в 02:48
source

1 answer

0

To create the new DataTable with the data that you request, this should help you (it would be necessary to know the real name of the columns or its exact index, but I think it will not cost you to replace the names that I have chosen for those that correspond) :

    // Diseñamos el nuevo DataTable con dos columnas, una para el nombre del producto
    // y otra para el volumen
    DataTable dtResultado = new DataTable();
    dtResultado.Clear();
    dtResultado.Columns.Add("Nombre", typeof(string));
    dtResultado.Columns.Add("Volumen", typeof(int));

    // Por cada producto...
    foreach (DataRow row in dtProductos.Rows)
    {
        // Creamos un nuevo row con el esquema definido anteriormente para dtResultado
        DataRow resultRow = dtResultado.NewRow();
        resultRow["Nombre"] = row["Nombre"].ToString();

        // Buscamos el volumen para ese producto en dtStock
        DataRow stockRow = dtStock.AsEnumerable()
                                  .Single(r => Convert.ToInt32(r["Codigo"]) == Convert.ToInt32(row["Codigo"]));

        resultRow["Volumen"] = Convert.ToInt32(stockRow["Volumen"]);

        // Añadimos nuevo registro al DataTable
        dtResultado.Rows.Add(resultRow);
    }

Now you only need to link dtResult to the grid after the loop.

    
answered by 05.01.2017 в 08:38