1

i am trying to display on a Windomws forms a datagridview. (for sudoku) my source is a table of integer.

i am using datasource but nothing displaying when i run.

public Form1()
        {
            InitializeComponent();

            int[,] my_table = new int[9, 9] {
                    { 2,1,8,3,5,7,6,4,9},
                    { 5,7,3,4,9,6,8,2,1},
                    { 6,9,4,1,8,2,3,5,7},
                    { 1,6,9,5,2,8,4,7,3},
                    { 3,5,2,9,7,4,1,8,6},
                    { 4,8,7,6,1,3,2,9,5},
                    { 7,3,5,2,4,1,9,6,8},
                    { 8,2,1,7,6,9,5,3,4},
                    { 9,4,6,8,3,5,7,1,2}
                };


            dataGridView1.DataSource = my_table.ToString();

            dataGridView1.Refresh();

            dataGridView1.Invalidate();
        }

my result is a grey square.

could you help me please ?

PS : sorry for my english, i'm french

kwal01
  • 11
  • 3
  • 1
    `my result is a grey square.` is because of `dataGridView1.DataSource = my_table.ToString();` TBH, create a new `DataTable` with 9 columns and nine records and then set that as the `DataSource`. – Trevor Oct 10 '19 at 13:44
  • thx, ".ToString()" is a test. you say 'create a new database' but many example use this dataGridView1.DataSource = my_table; – kwal01 Oct 10 '19 at 14:02
  • Create a new `DataTable` not database. – Trevor Oct 10 '19 at 14:12

3 Answers3

0

Try this:

GetRow method taken from : https://stackoverflow.com/a/51241629/1623971

private DataTable GetDTB(int count)
            {
                DataTable tb = new DataTable();
                for (int i = 1; i < count + 1; i++)
                {
                    tb.Columns.Add("C" + i);
                }
                return tb;
            }

            private DataTable FillDTB(int count = 9)
            {
                int[,] my_table = new int[9, 9] {
                        { 2,1,8,3,5,7,6,4,9},
                        { 5,7,3,4,9,6,8,2,1},
                        { 6,9,4,1,8,2,3,5,7},
                        { 1,6,9,5,2,8,4,7,3},
                        { 3,5,2,9,7,4,1,8,6},
                        { 4,8,7,6,1,3,2,9,5},
                        { 7,3,5,2,4,1,9,6,8},
                        { 8,2,1,7,6,9,5,3,4},
                        { 9,4,6,8,3,5,7,1,2}
                    };

                DataTable dtb = GetDTB(count);
                for (int i = 0; i < 9; i++)
                {
                    dtb.Rows.Add(GetRow(my_table, i));
                }

                return dtb;
            }


            public int[] GetRow(int[,] matrix, int rowNumber)
            {
                return Enumerable.Range(0, matrix.GetLength(1))
                        .Select(x => matrix[rowNumber, x])
                        .ToArray();
            }
demoncrate
  • 390
  • 2
  • 14
0

hi and thank for replyes but i dont understand,

why using datatable ? can t directly datasource my int[,] ?

i am trying that :

public Form1()
{
    InitializeComponent();

    int[,] my_table = new int[9, 9] {
                { 2,1,8,3,5,7,6,4,9},
                { 5,7,3,4,9,6,8,2,1},
                { 6,9,4,1,8,2,3,5,7},
                { 1,6,9,5,2,8,4,7,3},
                { 3,5,2,9,7,4,1,8,6},
                { 4,8,7,6,1,3,2,9,5},
                { 7,3,5,2,4,1,9,6,8},
                { 8,2,1,7,6,9,5,3,4},
                { 9,4,6,8,3,5,7,1,2}
            };


    dataGridView1.DataSource = FillDTB(my_table) ;

    dataGridView1.Refresh();


    for(int ii=0; ii<9; ii++)
    {
        for (int jj = 0; jj < 9; jj++)
        {
            richTextBox1.Text += my_table[ii, jj];
            if (jj != 8)
            {
                richTextBox1.Text += ".";
            }
        }
        richTextBox1.Text += Environment.NewLine;


    }


    // dataGridView1.Invalidate();

}



private DataTable GetDTB(int count)
{
    DataTable tb = new DataTable();
    for (int i = 1; i < count + 1; i++)
    {
        tb.Columns.Add("C" + i);
    }
    return tb;
}

private DataTable FillDTB(int[,] matrix, int count = 9)
 {


     DataTable dtb = GetDTB(count);
     for (int i = 0; i < 9; i++)
     {
         dtb.Rows.Add(GetRow(matrix, i));
     }

     return dtb;
 }//*/


public int[] GetRow(int[,] matrix, int rowNumber)
{
    return Enumerable.Range(0, matrix.GetLength(1))
            .Select(x => matrix[rowNumber, x])
            .ToArray();
}

in my richtext box it's ok i have my values. but in datagridview, i have juste "System.Int32[]" in first column and nothing in other.

what i miss ?

kwal01
  • 11
  • 3
0

OK, i have found.

public Form1()
{
    InitializeComponent();

    int[,] my_table = new int[9, 9] {

                { 8,2,3,4,5,9,1,6,7},
                { 6,9,7,8,1,2,5,6,4},
                { 5,1,4,6,7,3,2,9,8},
                { 7,4,1,3,9,6,8,2,5},
                { 3,6,5,1,2,8,7,4,9},
                { 2,8,9,5,4,7,6,1,3},
                { 1,5,2,7,3,4,9,8,6},
                { 9,3,8,2,6,5,4,7,1},
                { 4,7,6,9,8,1,3,5,2},

            };


    dataGridView1.DataSource = FillDTB(my_table) ;

    dataGridView1.Refresh();


    for(int ii=0; ii<9; ii++)
    {
        for (int jj = 0; jj < 9; jj++)
        {
            richTextBox1.Text += my_table[ii, jj];
            if (jj != 8)
            {
                richTextBox1.Text += ".";
            }
        }
        richTextBox1.Text += Environment.NewLine;


    }


     dataGridView1.Invalidate();

}

private DataTable GetDTB(int count)
{
    DataTable tb = new DataTable();
    for (int i = 1; i < count + 1; i++)
    {
        tb.Columns.Add();
    }
    return tb;
}

private DataTable FillDTB(int[,] matrix, int count = 9)
{


    DataTable dtb = GetDTB(count);

    for (int ii = 0; ii < count; ii++)
    {

        DataRow my_line;
        my_line = dtb.NewRow();

        for (int jj = 0; jj < count; jj++)
        {
            my_line[jj] = matrix[ii, jj];
        }

        dtb.Rows.Add(my_line);

    }

    return dtb;
 }//*/

this is work, but no reponse for why i can't use my 2 dimensional in datagridview directly.

thanks

kwal01
  • 11
  • 3