Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么dataRow总是包含第一行_C#_Foreach_Datatable - Fatal编程技术网

C# 为什么dataRow总是包含第一行

C# 为什么dataRow总是包含第一行,c#,foreach,datatable,C#,Foreach,Datatable,在我的项目中,我必须监控分散外围的状态。为此,我创建了一个类,该类镜像了一个具有名称、地址等的单个数据点。现在,我想将该类的所有单个对象插入SortedList。应该通过DataTable初始化DataPoint对象。我的问题是,在我的Foreach循环中,我总是拥有DataTable的前两个。因此,我创建了一个SortedList,在这个示例中,它是同一个条目的39倍。如果类DataPoint和DpPeriphyData类包含SortedList,则程序运行不会出现问题 private voi

在我的项目中,我必须监控分散外围的状态。为此,我创建了一个类,该类镜像了一个具有名称、地址等的单个数据点。现在,我想将该类的所有单个对象插入SortedList。应该通过DataTable初始化DataPoint对象。我的问题是,在我的Foreach循环中,我总是拥有DataTable的前两个。因此,我创建了一个SortedList,在这个示例中,它是同一个条目的39倍。如果类DataPoint和DpPeriphyData类包含SortedList,则程序运行不会出现问题

private void Cmd_Fbd_Data_List_Click(object sender, EventArgs e)
{
    ImportExportExcelToDataGridClass excel = new ImportExportExcelToDataGridClass();
    DataTable dataPointList = excel.ImportExceltoDataGrid(); 
    Dgv_Data_List.DataSource = dataPointList; 
    Dgv_Data_List.ReadOnly = true; 
    DpPeripheryData InputData = new DpPeripheryData(); 

    foreach (DataRow dataRow in dataPointList.Rows) 
    {
        DataPoint dataPoint = new DataPoint(dataRow); // making a new objekt 
        InputData.AddDataPoint(dataPoint); // add this class to the Sorted List 
    }
}

方法完成后,局部变量InputData似乎不会保留。它不会分配给方法之外的任何实例,因此一旦事件处理程序完成,它将被垃圾收集。除非
AddDataPoint
有你没有在这里分享的魔力。谢谢!将SortedList类列表设置为静态还是将InputData设置为主窗体的字段更好?我想这两种方法都可以,假设它们被分配给datagridview