2

I have created a multi-dimensional array:

string[,] array_questions = new string[dt.Rows.Count, dt.Columns.Count];

for (i = 0; i < dt.Rows.Count; i++)
{
    for (j = 0; j < dt.Columns.Count; j++)
    {
        array_questions[i, j] = dt.Rows[i][j].ToString();
    }
}

foreach (string number in array_questions)
{
    Response.Write(number + "\n");
}

But it displays the entire array in one single lengthy row. How can I display it row-wise in the aspx page?

3 Answers 3

4

Your problem is that foreach loop for rectangle two dimensional array will return all elements from that array one at a time. You need to use indexes to access rows and columns of a two-dimensional array.

Go through each row and display each element. Then add paragraph (new line) after each row.

Example is given below:

for (int row = 0; row < array_questions.GetLength(0); row++)
{
    for (int column = 0; column < array_questions.GetLength(1); column++)
    {
        //writing data, you probably want to add comma after it
        Response.Write(array_questions[row,column]); 
    }

    //adding new line, so that next loop will start from new line
    Response.Write(Environment.NewLine);
} 

for array of 5 rows and 10 columns of default int values, I've received next table

0000000000
0000000000
0000000000
0000000000
0000000000

If you had correctly populated array_questions before, you should receive table-view data on page, resulted in Response.Write calls.


A much cleaner solution would be to reuse dt (I assume it's a DataTable) Rows property, which is IEnumerable<DataRowCollection>. Next code achieves similar behavior, but in much cleaner fashion, and you don't need another array.

foreach (var row in dt.Rows)
{
    Response.Write(string.Join(", ", row) + Environment.NewLine);
}

will print data in next table-like fashion:

0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Sign up to request clarification or add additional context in comments.

5 Comments

Alternatively you could string.Join somehow. I'm not sure exactly what you would give it as the array-parameter though. Maybe an ArraySegment could work?
@Alxandr yes, string.Join would work perfectly on a jagged array, but in case of rectangle array, there is no build-in mechanism to retrieve separate rows and lines. ArraySergment works on one-dimensional array (or jagged arrays, which is basically a one-dimensional array of one-dimensional arrays).
@IlyaIvanov you are right. I looked it up. Though I must say there should be a structure for this (like ArraySegment, but maybe something like ArrayDimention or whatever). And string.Join takes an enumerable (though it's probably not as efficient as giving it an array). Your double loop is probably the best solution still though.
@ Ilya Ivanov but it shows an Error :The best overloaded method match for 'string.Join(string, string[])' has some invalid arguments ..please help
@user2431727 Then use the first options.
2
for (int r = 0; r < dt.Rows.Count; r++)
{
    for (int c = 0; c < dt.Columns.Count; c++)
    {
        Response.Write(String.Join(", ", dt.Rows[r][c].ToString())); 
    }
    Response.Write("<br />");
}

2 Comments

as far as I understood, OP want's actual values of array_questions formatted in table, not the count of each dimension.
it show Error : The best overloaded method match for 'string.Join(string, string[])' has some invalid arguments
1

what You think about this way?

   for (int r = 0; r < table.GetLength(0); r++)
    {
        for (int k = 0; k < table.GetLength(0); k++)
        {
            Console.Write((table[r, k] + " " ));
        }
        Console.Write(Environment.NewLine + Environment.NewLine);
    }
    Console.ReadLine();

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.