How do I fill the columns of my datatable with different string type lists in C #?

0

I would like to know what is the correct way to properly fill a datatable I have, the question is that I must fill it with different lists where I have my information, now my datatble looks like this:

What I want is that column 2 is at the same level as my column 1.

Code C #:

 List<List<string>> LstList = new List<List<string>>();
            LstList.Add(lstUsing.Items.Cast<string>().ToList());
            LstList.Add(lstExternal.Items.Cast<string>().ToList());
            LstList.Add(lstFunctions.Items.Cast<string>().ToList());
            LstList.Add(lstMe.Items.Cast<string>().ToList());

            DataTable dtSim = new DataTable();


            for (int i = 0; i < LstList.Count; i++)
            {
                    dtSim.Columns.Add("Col "+(i+1));
                if (i == 0) {
                    for (int x = 0; x < LstList.ElementAt(i).Count; x++)
                    {
                        DataRow dr = dtSim.Rows.Add();
                        dr.SetField("Col 1", lstUsing.Items.Cast<string>().ToList().ElementAt(x));
                    }
                } else if (i==1) {
                    for (int x = 0; x < LstList.ElementAt(i).Count; x++)
                    {
                        DataRow dr = dtSim.Rows.Add();
                        dr.SetField("Col 2", lstExternal.Items.Cast<string>().ToList().ElementAt(x));
                    }
                }



            }
    
asked by David 02.01.2019 в 22:38
source

2 answers

1

I think you can do this

DataTable table = new DataTable();

List<List<int>> items = new List<List<int>>
{
    new List<int> {1, 2, 3, 4 },
    new List<int> { 5,6,7,8 },
    new List<int> {4,6,8,0 }
};

// Obtener la lista más larga(Filas)
int maxRowCount = items.Max(x => x.Count);

// Anadir una columna para poder agregar todas la filas
table.Columns.Add();

// Agregando todas las filas en 0
for (int i = 0; i < maxRowCount; i++)
    table.Rows.Add(0);

// Recorrer tu lista como una Matriz 
for (int col = 0; col < items.Count; col++)
{
    for (int row = 0; row < items[col].Count; row++)
    {
        // Asignar valor a cada celda
        table.Rows[row].SetField(col, items[col][row]);
    }
    table.Columns.Add();
}

// Esto es para mostrar los datos en consola
foreach (DataRow row in table.Rows)
{
    foreach (var item in row.ItemArray)
    {
        Console.Write("\t {0}", item);
    }
    Console.WriteLine();
}

Console.Read();
    
answered by 02.01.2019 / 23:23
source
0

After adding the data in "Col1" you must go back to DataTable dtSim to fill the information in the "Col2".

This is your modified code to achieve what I describe in previous lines:

List<List<string>> LstList = new List<List<string>>();
LstList.Add(lstUsing.Items.Cast<string>().ToList());
LstList.Add(lstExternal.Items.Cast<string>().ToList());
LstList.Add(lstFunctions.Items.Cast<string>().ToList());
LstList.Add(lstMe.Items.Cast<string>().ToList());

DataTable dtSim = new DataTable();

// Entiendo que aquí estás agregando columnas al DataTable "dtSim"...
for (int i = 0; i < LstList.Count; i++)
{
    dtSim.Columns.Add("Col "+(i+1));
}

// Agregar información en la columna "Col1".
for (int i = 0; i < LstList.Count; i++) 
{
    for (int x = 0; x < LstList.ElementAt(i).Count; x++)
    {
        DataRow dr = dtSim.Rows.Add();
        dr.SetField("Col 1", lstUsing.Items.Cast<string>().ToList().ElementAt(x));
    }
}

// Agregar información en la columna "Col2".
for (int i = 0; i < LstList.Count; i++) 
{
    for (int x = 0; x < LstList.ElementAt(i).Count; x++)
    {
        DataRow dr = dtSim.Rows.Add();
        dr.SetField("Col 2", lstExternal.Items.Cast<string>().ToList().ElementAt(x));
    }
}
    
answered by 02.01.2019 в 23:00