0

I have 2 collection's one within the other like this,

public class Meter
    {
        public string MeterID { get; set; }
        public List<Data> data { get; set; }
    }

 public class Data
    {
        public string Time { get; set; }
        public int Signal { get; set; }
    }

and I read it from a txt file like this,

    public static void LoadMeterListFromFile(List<FileInfo> fileList)
    {
        foreach (FileInfo fi in fileList)
        {
            foreach (var line in File.ReadAllLines(fi.FullName))
            {
                var columns = line.Split(';');                      

                string MeterID = columns[1];

                if (!meters.ContainsKey(MeterID))
                {
                    meters.Add(MeterID, new Meter() { MeterID = MeterID, data = new List<Data>() });
                }
                Data d = new Data
             {
                 TimeStamp = columns[0],
                 Signal = Convert.ToInt32(columns[2].Replace("SignalStrength=", "")),

             };
                meters[MeterID].data.Add(d);
            }
        }
    }

How do i show both collection's within a wpf datagrid, preferably Grouped by MeterID?

Mario Stoilov
  • 3,411
  • 5
  • 31
  • 51
user3240428
  • 111
  • 1
  • 2
  • 8

1 Answers1

0

You should consider changing the way you read the data and add MeterID to class Data.

public static void LoadMeterListFromFile(List<FileInfo> fileList)
{
    foreach (FileInfo fi in fileList)
    {
        foreach (var line in File.ReadAllLines(fi.FullName))
        {
            var columns = line.Split(';');
            string meterID= columns[1];

            if (!meters.ContainsKey(MeterID))
            {
                meters.Add(meterID, new Meter() { MeterID = meterID, data = new List<Data>() });
            }
            Data d = new Data
            {
                MeterID = meterID,
                TimeStamp = columns[0],
                Signal = Convert.ToInt32(columns[2].Replace("SignalStrength=", ""))
            };
            meters[MeterID].data.Add(d);
        }
    }
}

Then, to group the results by MeterID and add them to DataGrid:

public MyForm()
{
    InitializeComponent();

    // Get meters list

    var result = new List<Data>();
    foreach(var m in meters)
        result.AddRange(m.data);

    // the records should be grouped already, but to be sure you can use linq
    result.GroupBy(d => d.MeterID);

    // now add data to data grid
    BindingList<Data> data = new BindingList<Data>(result);
    dataGridView1.DataSource = data;
  • thx for the help. I have changed TimeStamp to DateTime becouse I need it in a Wpf Chart, but that means I need TimeStamp to be converted to DateTime but i keep getting the "string not recognised as a valid DateTime". How can i fix that? – user3240428 Feb 19 '14 at 13:08
  • The error means, there is no default conversion from string you provide to datetime. To define and convert your string format with `DateTime.ParseExact()` method. You can find a sample [here](http://stackoverflow.com/a/919276/2404858). – michal.pawlowski Feb 19 '14 at 13:54