1

I have created new column against every new value of N now it displays only the last column of every row also the column name is started from '0' I want to start it from '1'

private void printBoard(int[,] board,int N)
{
    for (int i = 0; i < N; i++)
    {
          DataGridViewRow row = new DataGridViewRow();              
        for (int j = 0; j < N; j++)
        {
            if (i > 0 || j > 0)
            {
                dataGridView1.ColumnCount = N;
                row.CreateCells(dataGridView1);                  
                dataGridView1.Columns[j].Name = j.ToString();
                    row.Cells[j].Value = board[i, j];
            }
        }
       this.dataGridView1.Rows.Add(row);
    }
}

Here is the output:

display 2d array in DataGridView

1
  • While you can show a 2d array manually in DataGridView, but it's better to use a DataTable to have data-binding. Also you don't need to pass N to the method, it should be the upper bound of 2nd dimention. Commented Dec 21, 2019 at 13:45

2 Answers 2

1

You don't need to specify N as parameter of the method. You should get the number of columns and rows from bounds of the array using GetUpperBound method.

I changed the signature of the method a bit, to pass the DataGridView and the data:

private void printBoard(DataGridView dgv, int[,] board)
{
    var columns = board.GetUpperBound(1) + 1; //Number of columns
    var rows = board.GetUpperBound(0) + 1;    //Number of rows

    //Add columns (name, text)
    for (int c = 0; c < columns; c++)
    {
        dgv.Columns.Add($"{c + 1}", $"{c + 1}"); 
    }
    //Add rows
    for (int r = 0; r < rows; r++)
    {
        //Slice 2d array and get the row
        var row = Enumerable.Range(0, columns).Select(c => (object)board[r, c]).ToArray();
        //Add the row
        dgv.Rows.Add(row);
    }
}

And here is an example of the usage:

private void button1_Click(object sender, EventArgs e)
{
    var a = new int[,] { { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 }, { 4, 4, 4 } };
    printBoard(dataGridView1, a);
}

Note: In general, it's better to use data-binding when it's possible. For example if instead of a 2d array, you set a DataTable as DataSource of the DataGridView, then when you edit the cells, the changes will be applied on the DataTable, however for the 2d array, you need to export data again into a 2d array manually.

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

5 Comments

well it worked aswell but its bit complicated so i went with other solution .. still thanks for early response :)
No worries, The choice is yours :) In fact in this answer I tried to show a few points, the way that you can add columns and set their text, the way that you can add rows by passing an array to Rows.Add, and the way that you can slice a 2d array to rows.
BTW, JFYI, you can upvote useful answers including the accepted one by clicking on up arrow near the answer. To learn more, take a tour.
i have done that sorry for the late response i was bit off
stackoverflow.com/questions/59863686/… here im stuck in a problem aswell can you help me out
1

Initialize columns and rows before filling board cells:

private void printBoard(int[,] board, int N)
{
    // create columns
    dataGridView1.ColumnCount = N;
    for (int c = 0; c < N; c++)
        dataGridView1.Columns[c].Name = c.ToString();

    // create N empty rows
    dataGridView1.Rows.Add(N);

    // fill cells
    for (int r = 0; r < N; r++)
    {
        for (int c = 0; c < N; c++)
        {              
            dataGridView1[c, r].Value = board[r, c];
        }
    }
}

1 Comment

stackoverflow.com/questions/59863686/… can you help me over this problem please i shall be thankful

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.