简体   繁体   English

无法在C#中明智地访问2d字符串数组行

[英]Unable to access 2d String Array row wise in C#

I am taking the data into a 2d string in C#. 我将数据放入C#中的2d字符串中。 Defined as :: 定义为 ::

string[,] strValueBasic = new string[GridView1.Rows.Count,49];

Now i want the data to be taken into DataRows and add these rows into the DataTable. 现在,我希望将数据带入DataRows并将这些行添加到DataTable中。

     for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    DataRow drRow = dtNew.NewRow();
                    drRow.ItemArray = strValueBasic[i];
                    dtNew.Rows.Add(drRow);
                }

But i am not able to access this 2d String array in the step 但是我无法在步骤中访问此二维字符串数组

                    drRow.ItemArray = strValueBasic[i];

It is giving error as Wrong number of indices inside [], expected 2. 由于[]中的索引数目错误,预期会出现错误2。

I know that it requires 2 indices, but than i want the whole row of that string array to be filled inside that DataRow. 我知道它需要2个索引,但比起我想将该字符串数组的整个行填充到该DataRow中。

Also the structure of DataRow and String Array is same. DataRow和字符串数组的结构也相同。

If you want to assign whole row your strValueBasic should be defined as string[][] . 如果要分配整行,则应将strValueBasic定义为string[][] Not the 2D array but array of arrays. 不是二维数组,而是数组数组。 That changes initialization too, each array can have different size so you have to create each separately. 这也改变了初始化,每个数组可以具有不同的大小,因此您必须分别创建每个数组。 http://msdn.microsoft.com/en-us/library/2s05feca.aspx http://msdn.microsoft.com/zh-CN/library/2s05feca.aspx

Thats because strValueBasic is a 2D array and you're trying to access it as it's a 1D array. 那是因为strValueBasic是2D数组,而您试图访问它是1D数组。

 for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                DataRow drRow = dtNew.NewRow();
                drRow.ItemArray = strValueBasic[i, iSomeOtherValue];
                dtNew.Rows.Add(drRow);
            }

Notice iSomeOtherValue , you're missing something! 注意iSomeOtherValue ,您缺少了一些东西!

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

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