将项目添加到列表<&燃气轮机;c#
我有一个2D数组,我正试图将其写入一个列表中,以便可以将其与datagrid绑定。 下面是我的代码将项目添加到列表<&燃气轮机;c#,c#,visual-studio,silverlight-4.0,C#,Visual Studio,Silverlight 4.0,我有一个2D数组,我正试图将其写入一个列表中,以便可以将其与datagrid绑定。 下面是我的代码 string[,] array = new string[8,4]; array[counting2, 0] = txtSend.Text; array[counting2, 1] = One; array[counting2, 2] = Two; array[counting2, 3] = Three;
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;
string[,]array=新字符串[8,4];
数组[counting2,0]=txtSend.Text;
数组[计数2,1]=一;
数组[计数2,2]=2;
数组[计数2,3]=三;
List Hello1=新列表();
测试Hello=新测试();
对于(int i=0;i移动
Testing Hello = new Testing();
圈内
所以你有
for (int i = 0; i <= counting2;i++ )
{
Testing Hello = new Testing();
Hello.year = array[counting2, 0];
Hello.One = array[counting2, 1];
Hello.Two = array[counting2, 2];
Hello.Three = array[counting2, 3];
Hello1.Add(Hello);
}
for(int i=0;i问题与您所说的完全一样:您将同一元素添加到列表中三次。每次迭代都会更改它,但它始终是同一个对象。您应该将对象的创建移动到循环中,以便每次都创建不同的对象
for (int i = 0; i <= counting2;i++ )
{
Testing Hello = new Testing();
Hello.year = array[counting2, 0];
Hello.One = array[counting2, 1];
Hello.Two = array[counting2, 2];
Hello.Three = array[counting2, 3];
Hello1.Add(Hello);
}
for(inti=0;i按如下方式更改代码它将起作用。您需要在其中实例化对象,以便它每次都是新的
for (int i = 0; i <= counting2;i++ )
{
Testing Hello = new Testing();
Hello.year = array[counting2, 0];
Hello.One = array[counting2, 1];
Hello.Two = array[counting2, 2];
Hello.Three = array[counting2, 3];
Hello1.Add(Hello);
}
for(int i=0;i