Search code examples
c#listviewdatagrid

I cannot show my data in a DataGrid with a ListView


I'm trying to fill a datagrid, but it doesn't work and I get this error:

CS1922 The type SPSDataBlock cannot be initialized with a collections initializer because it is not System.Collections.IEnumerable

Here is the code:

private void DisplaySPS()
{
    this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, new Action(delegate ()
    {
        List<SPSDatenbautein> items = new List<SPSDatenbautein>();

        items.Add(new SPSDatenbautein()
        {
            _sPSDatenbautein.All_processes,
            _sPSDatenbautein.Magnetisierung,
            _sPSDatenbautein.Hypot,
            _sPSDatenbautein.Continuity,
            _sPSDatenbautein.Gesamt_Isolationswiderstand,
            _sPSDatenbautein.Gesamt_Leckstrom,
            _sPSDatenbautein.Benutzer_Anmeldung,
            _sPSDatenbautein.Material_number,
            _sPSDatenbautein.Order_number,
            _sPSDatenbautein.Serial_number,
            _sPSDatenbautein.Continuity_Measurment_Result,
            _sPSDatenbautein.Magnetisierung_Measurment_Result,
            _sPSDatenbautein.Strom_Leistung,
            _sPSDatenbautein.Isolationswiderstand,
            _sPSDatenbautein.Leckstrom)
        };

        SPSDataGrid.ItemsSource = items;
    }));
}

Solution

  • I assume your SPSDatenbaustein contains all the properties you are trying to copy from another object in your new SPSDatenbaustein() call.

    In that case it should look like more than this:

    items.Add(new SPSDatenbautein() {
           All_processes =    _sPSDatenbautein.All_processes,
           Magnetisierung = _sPSDatenbautein.Magnetisierung,
           Hypot = _sPSDatenbautein.Hypot,
                        //etc..
    } ) ;
    

    The error explains itself, because of the way you do it you can actually prefill IEnumerations like lists:

    List<string> s = new List<string>() {"first", "second"};