我有一个2D数组,我试图写入一个List,所以我可以绑定它与数据网格.下面是我的代码.
string[,] array = new string[8,4];
array[counting2, 0] = txtSend.Text;
array[counting2, 1] = One;
array[counting2, 2] = Two;
array[counting2, 3] = Three;
List<Testing> Hello1 = new List<Testing>();
Testing Hello = new Testing();
for (int i = 0; i <= counting2;i++ )
{
Hello.year = array[counting2, 0];
Hello.One = array[counting2, 1];
Hello.Two = array[counting2, 2];
Hello.Three = array[counting2, 3];
Hello1.Add(Hello);
}
dataGrid1.ItemsSource = Hello1;
Run Code Online (Sandbox Code Playgroud)
可以看到的是,当我的数组包含3行时,数据网格显示3行,具有相同的数据,而不是3个不同的数据.我猜是我将Hello添加到列表中3次.
但是我是否将Hello更改为varbele,因此每次for循环都会循环另一个名称.
Ne Ideas ??