1

I am trying to create a 2D array from a DataTable, however Instead of getting the end result defined below. One row is not added, if that row is added, it will overwrite the column values.

Here is the end result I am looking to get:

    [
        //columns
['TaskID','TaskName','StartDate','EndDate','Duration','PercentComplete','Dependecies'],

//rows
        ['Cite','Create bibliography',null,1420649776903,1,20,'Research'], //missing row
        ['Complete','Hand in paper',null,1420908976903,1,0,'Cite,Write'],
        ['Outline','Outline paper',null,1420563376903,1,100,'Research'],
        ['Research','Find sources',1420390576903,1420476976903,null,100,null],
        ['Write','Write paper',null,1420822576903,3,25,'Research,Outline']
    ];

However, I get this.

[
    ["TaskID","TaskName","StartDate","EndDate","Duration","PercentComplete","Dependecies"],
    ["Complete","Hand in paper",null,1420908976903.0,1,0,"Cite,Write"],
    ["Outline","Outline paper",null,1420563376903.0,1,100,"Research"],
    ["Research","Find sources",1420390576903.0,1420476976903.0,null,100,null],
    ["Write","Write paper",null,1420822576903.0,3,25,"Research,Outline"]

]

I have tried:

 // define the array size based on the datatable
            object[,] multiDimensionalArry = new Object[breDataTable.Rows.Count, breDataTable.Columns.Count];
            const int columnsArryIndex = 0;

            // populate array with column names
            if (breDataTable.Columns.Count > 0 && breDataTable.Rows.Count > 0)
            {
                // loop around columns
                for (int i = 0; i <= breDataTable.Columns.Count - 1; i++)
                {
                    // add all the column names
                    multiDimensionalArry[columnsArryIndex, i] = breDataTable.Columns[i].ColumnName;
                }

            }

            // outer loop - loop backwards from the bottom to the top
            // we want to exit the loop, when index == 0 as that is reserved for column names
            for (int j = breDataTable.Rows.Count - 1; j >= 0; j--)
            {
                // get current row items array to loop through
                var rowItem = breDataTable.Rows[j].ItemArray;

                // inner loop - loop through current row items, and add to resulting multi dimensional array
                for (int k = 0; k <= rowItem.Length - 1; k++)
                {
                       multiDimensionalArry[j, k] = rowItem[k];
                }
            }

1 Answer 1

1

You're missing one row when creating your array since the columns name row dosent count as a row

object[,] multiDimensionalArry = new Object[breDataTable.Rows.Count + 1, breDataTable.Columns.Count];

And you need to change your for loop to take into count the extra row

// outer loop - loop backwards from the bottom to the top
// we want to exit the loop, when index == 0 as that is reserved for column names
for (int j = breDataTable.Rows.Count - 1; j >= 0; j--)
{
    // get current row items array to loop through
    var rowItem = breDataTable.Rows[j].ItemArray;

    // inner loop - loop through current row items, and add to resulting multi dimensional array
    for (int k = 0; k <= rowItem.Length - 1; k++)
    {
        multiDimensionalArry[j + 1, k] = rowItem[k];
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks this is what i was trying to do, friday evening got my brain fried :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.