2015-05-06 44 views
0

我需要将三个List添加到一个数据网格。每个列表应该有它自己的行。这些列表只会被创建/更改一次 - 这是在从CSV文件导入数据的过程中。我知道如何使用以下两个代码列表添加到DataGrid:将三个列表<string>添加到WPF数据网格

dtgCsvData.ItemsSource = time.Zip(temperature, (t, c) => new { time = t, temperature = c }); 

如果时间和温度是两个三个列表中。第三个也是最后一个列表被命名为rate。

public List<string> temperature; 
public List<string> time; 
public List<string> rate; 

但是,我看不到我如何将最后一个列表添加到数据网格。我该怎么做?

回答

0

如果他们都是相关的,干嘛还要有三个不同的名单?他们也应该一起生活。我的意思是创建一个具有所有必要属性的类。

public class TemperatureEntity 
{ 
    public double Temperature {get; set;} 
    public DateTime Time {get; set;} 
    public string Rate{get; set;}//string or whatever type it is 
} 

所有三个列表现在将成为TemperatureEntity

public List<TemperatureEntity> temperatures; 

一个名单,然后将其绑定到数据网格。

dtgCsvData.ItemsSource = temperatures; 
+0

如何将数据添加到TemperatureEntity类?现在我使用以下内容:'temperature = new List (reader.GetTemperature());' 'time = new List (reader.GetTime());' 'rate = new List (reader.GetRate ));' – MikaelKP

+0

@MikaelKP你的'读者'是什么?您需要创建'TemperatureEntity'的新实例并将其添加到列表中。 'temperature.Add(new TemperatureEntity(){Temperature = temperatureValue,Time = dateTime})' –

2

可以使用复合材料收集到所有你的列表绑定到数据网格

这样的 -

<DataGrid.ItemsSource> 
       <CompositeCollection> 
        <CollectionContainer Collection="{Binding temperature}" /> 
        <CollectionContainer Collection="{Binding time}" /> 
        <CollectionContainer Collection="{Binding rate}" /> 
       </CompositeCollection> 
</DataGrid.ItemsSource> 
相关问题