1

I have a DataTable which consists of dynamic rows:

no   |   cid   | wait  |  prio
--------------------------------
1    |   1234  |  0:57 |    0
2    |   54785 |  0:44 |    0
3    |   74125 |  0:22 |    0

I want to create an array of arrays from this, having information of each row in an individual array.

12
  • 6
    The implementation of a DataTable is an array of arrays. The Rows property is the outer array and then each row has an ItemArray which is an object[] of values for that row. Commented Jun 5, 2013 at 14:21
  • Can you code your statement as I am very new in .Net. Commented Jun 5, 2013 at 14:26
  • Is this a Microsoft SQL Server? Commented Jun 5, 2013 at 14:27
  • @MichaelPerrenoud It doesn't matter once it's in a data table where the data came from... Commented Jun 5, 2013 at 14:27
  • 2
    Your data table already has everything you want, creating separate arrays is just adding pain to your process. I would suggest researching "accessing data from DataTables". Commented Jun 5, 2013 at 14:29

4 Answers 4

8

Although you don't need this

var arr = dataTable.Rows.Cast<DataRow>().Select(r => r.ItemArray).ToArray();
Sign up to request clarification or add additional context in comments.

Comments

3

Although I applaud other users for answering your direct question regarding an array of array. I think you are making this more difficult than you need to.

    yourDataTable.Rows[rowIndex]["no"]

is already an array containing all the values in your "no" column.

    yourDataTable.Rows[rowIndex]["cid"]

is already an array containing all the values in your "cid" column.

etc... etc..

You can access the data by either looping through the rows, or calling one directly in place of rowIndex as shown above, and assigning the value in the column to some other variable.

If you really want to get arrays of arrays (which you already have), then disregard this and refer to the other answers.

4 Comments

Though I'm only assuming here, the OP is likely sending this data to another assembly (i.e. unmanaged C or something like that) and it only supports an array.
I considered that, but also felt that perhaps he does not know how to access the data directly, so thought it was worth a mention.
@MichaelPerrenoud While that's a possibility, that doesn't appear to be the case here
Hey now, don't start arguing in comments on my answer please ;)
1

Something like this would probably work as intended:

List<object> list = new List<object>();
foreach (object row in DataGridView.Rows)
    {
    List<object> newList = new List<object>();
    list.Add(newList);
    foreach (object columns in row)
    {
        newList.Add(columns.DataBoundItem);
    }
    }

Comments

-4

There is no need to create array for data tables.

Comments

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.