简体   繁体   English

C#2D数组进入DataGrid视图

[英]c# 2d array in to datagrid view

I'm trying to display the 2d array in datagridview. 我正在尝试在datagridview中显示2d数组。 Im generting data via Windows Form into 2d array and then trying to display that data into datagrid view but I get an error on line row.CreateCells(this.dataGridView1); 我通过Windows窗体将数据生成到2d数组中,然后尝试将该数据显示到datagrid视图中,但是在行row.CreateCells(this.dataGridView1)上出现错误 Row provided already belongs to a DataGridView control. 提供的行已经属于DataGridView控件。 not sure what im doing wrong appreciate if anyone please put me on the right direction Code is below 不知道我做错了什么,如果有人将我带入正确的方向,请欣赏下面的代码

   iArray = new String[2, 10];
            iArray = custDetails.pCustomDetails();


            int height = iArray.GetLength(0);
            int width = iArray.GetLength(1);
        MessageBox.Show(height.ToString());
        MessageBox.Show(width.ToString());

        this.dataGridView1.ColumnCount = width;

            for (int r = 0; r < height; r++)
            {

            row.CreateCells(this.dataGridView1);

                for (int c = 0; c < width; c++)
                {
                row.Cells[c].Value = iArray[r, c];

                }

                this.dataGridView1.Rows.Add(row);
            }

The only problem here is that you are trying to add the same instance of row again and again in loop. 唯一的问题是,您试图一次又一次地循环添加同一实例

So this small fix will make yr code work 因此,此小修正将使您的代码正常工作

 for (int r = 0; r < height; r++)
        {

         //Fix : create a new instance of row every time
          DataGridViewRow row = new DataGridViewRow();


        row.CreateCells(this.dataGridView1);

            for (int c = 0; c < width; c++)
            {
            row.Cells[c].Value = iArray[r, c];

            }

            this.dataGridView1.Rows.Add(row);
        }

I think this answer is the best one you can find, just rewrite it into C#: 我认为此答案是您可以找到的最佳答案,只需将其重写为C#:

'at first add columns to the dataGridView before insert rows in it
'y = number of the columns which should equal yourArray.getlength(0) - 1 
'you can change (col) to any type of column you need

        For i = 0 To y
            Dim col As New DataGridViewTextBoxColumn
            col.DataPropertyName = "PropertyName" & i.ToString
            col.HeaderText = i.ToString
            col.Name = "colWhateverName" & i.ToString
            DataGridView1.Columns.Add(col)
        Next

'now you insert the rows
'exampleRow ={yourArray(0,0), yourArray(0,1), ..... ,yourArray(0,y)}
'x = number of rows

        For i = 0 To x
            Dim aarray As New List(Of String)
            For ii = 0 To y
                aarray.Add(yourArray(i, ii).ToString)
            Next
            DataGridView1.Rows.Add(aarray.ToArray)
        Next

I do such an app you can check it here: https://github.com/ammardab3an/Array-Rotate-90 我做了一个这样的应用程序,您可以在这里检查它: https : //github.com/ammardab3an/Array-Rotate-90

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM