Home > Software engineering >  I cannot show my data in a DataGrid with a ListView
I cannot show my data in a DataGrid with a ListView

Time:07-12

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;
    }));
}

CodePudding user response:

I assume you SPSDatenbaustein contains all 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 the way you do it you can actually prefill IEnumerations like lists:

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

CodePudding user response:

A working code may look like this:

SPSDataGrid.ItemsSource = new List<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
};
  • Related