Edición de DataTable por fila a través de la declaración foreach

My problem is with my inability to fill a DataTable with values of another Datatable. Code below:


        foreach (DataRow row1 in ds.Tables["tests"].Rows)
        {
            //Use value stored in DataTable "tests" to generate
            //new DataTable called "temp" and fill it with values

            foreach (DataRow row2 in ds.Tables["temp"].Rows)
            {
                //use values in "temp"-table to fill fields in "tests"-table
       ->       if (specific condition in Datatable "tests" is met)
                {
                    example1: row1.ItemArray[10] = Convert.ToInt64(row2.ItemArray[0]);
                    example2: ds.Tables["tests"].Rows[row1.Table.Rows.IndexOf(row1)].ItemArray[10] = Convert.ToInt64(row2.ItemArray[0]);
                }
                else if (another specific condition in Datatable "tests" is met)
                {
                    //Different fields will be filled
                }
            }
        }

When attempting to debug the program, using a stop-point at the start of the if-statement everything resolves, until the program jumps into the execution block of the if-statement.

Every line in the execution block is skipped.
Here's the readout message i get (loosely translated):
NonUserCode "System.Data.DataSet.Tables.get" is being skipped.
NonUserCode "System.Data.DataRow.ItemArray.get" is being skipped.

Cualquier ayuda sería apreciada. ¡Gracias!

preguntado el 28 de agosto de 12 a las 11:08

I don't understand your code. First you fill the 'temp' table using values of 'test' table, then you use the values in 'temp' table to change values in 'test' table. Could you be more specific on how you fill the 'temp' table and which are the conditions to fill back the test table? -

2 Respuestas

No need to work with ItemArray.

if (specific condition in Datatable "tests" is met)
{
      row1[10] = Convert.ToInt64(row2[0]); //try this only.
}
else if (another specific condition in Datatable "tests" is met)
{
     //Different fields will be filled
}

When you are accessing ItemArray it return a local array. and by assigning value to this local array, not making any change in datatable.

Respondido 28 ago 12, 11:08

It's because ".ItemArray" returns copy of internal values. - TcKs

Try to review topic: DataRow Copy

Respondido 28 ago 12, 11:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.