Search code examples
c#.netwpfdatagridwpfdatagrid

WPF: Adding to a datagrid from various Lists


I have various lists of values that I am reading in through a text file for a data logging application. My end goal is to have a Data grid with a column for each value. Here is a basic outline of what I am trying to do...

1.) A text file is read in and a new dataFile class is created - This class parses the various columns in the text file and creates Lists of floats for each variable.

2.) The dataFile class contains methods to return each of these lists

3.) I want to add these lists to my data grid but I have not been able to find information on how to do this.

Example: I can get ONE column to fill be using the following line of code...

dataGrid1.ItemsSource = runOne.getRPM();

Where runOne is a fataFile, and getRPM() returns a list of floats.

When I try to do this with more than one list, e.g.

 dataGrid1.ItemsSource = runOne.getRPM();
 dataGrid1.ItemsSource = runOne.getPower();

Only the first column of the dataGrid is filled. I have already created the columns for the dataGrid and below is the XAML code...

<DataGrid AutoGenerateColumns="False" Height="305" HorizontalAlignment="Left"   Margin="153,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="454">
        <DataGrid.Columns>
            <DataGridTextColumn Header="RPM" />
            <DataGridTextColumn Header="Flywheel Torque" />
            <DataGridTextColumn Header="TorqM" />
            <DataGridTextColumn Header="TorqS" />
            <DataGridTextColumn Header="Power (HP)" />
            <DataGridTextColumn Header="Flywheel Power" />
            <DataGridTextColumn Header="Temp" />
        </DataGrid.Columns>
    </DataGrid>

Is there a way that I can fill each column based on items from individual lists? I wrote a method to return all of the lists (a List of Lists) and I tried to get that to work, but it did not.

Any suggestions/ideas are greatly appreciated.


Solution

  • Try to create and fill an observable collection with a new class where you will have two properties for RPM and Power (ObservableCollection<RpmPower>). After that you can bind the grid to the new collection.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    
    namespace Test
    {
        public class RpmPowerModel : INotifyPropertyChanged
        {
            #region Properties
    
            public int Rpm
            {
                get { return _rpm; }
                set
                {
                    if (_rpm != value)
                    {
                        _rpm = value;
                        RaisePropertyChanged("Rpm");
                    }
                }
            }
    
            private int _rpm;
    
            public int Power
            {
                get { return _power; }
                set
                {
                    if (_power != value)
                    {
                        _power = value;
                        RaisePropertyChanged("Power");
                    }
                }
            }
    
            private int _power;
    
            #endregion
    
            #region Implementation of INotifyPropertyChanged
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void RaisePropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
            }
    
            #endregion
    
            public static List<RpmPowerModel> LoadItems(List<int> rpms, List<int> powers)
            {
                if (rpms == null)
                {
                    throw new ArgumentNullException("rpms");
                }
    
                if (powers == null)
                {
                    throw new ArgumentNullException("powers");
                }
    
                var result = new List<RpmPowerModel>(Math.Max(rpms.Count, powers.Count));
    
                for (int i = 0; i < Math.Max(rpms.Count, powers.Count); i++)
                {
                    var model = new RpmPowerModel
                    {
                        Rpm = i < rpms.Count ? rpms[i] : 0,
                        Power = i < powers.Count ? powers[i] : 0
                    };
    
                    result.Add(model);
                }
    
                return result;
            }
        }
    }