Create Lists with BindingList C #

2

code to create a bindingList:

using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using Negocio;
using Entidades;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Grid;  

private BindingList<EntidadEncabezado> CargarEncabezados()
    {
        Cuenta en = new Cuenta();
        BindingList<EntidadEncabezado> ee = new BindingList<EntidadEncabezado>();
        BindingList<EntidadEncabezado> tempee = new BindingList<EntidadEncabezado>();
        int IdEntidad = 0;
        ee.AllowNew = true;
        if (gridView2.SelectedRowsCount > 0)
        {
            int i = 0;
            int[] Handles = ((GridView)gridCuentas.MainView).GetSelectedRows(); //Obtengo las filas seleccionadas en el gridView y las almaceno en un array.

            while ( i < gridView2.SelectedRowsCount) //--> Recorro el gridView. 'i' Me da el índice de la fila del gridView
            {
                // A un objeto de tipo 'Cuenta' le concateno la fila seleccionada. Al array le paso el indice de la fila actual.
                en = (Cuenta)(gridCuentas.MainView.GetRow(Handles[i]));
                IdEntidad = en.Entidad.IdEntidad; // Obtengo el IdEntidad para pasar como parámetro en el método 'EntidadEncabezado'.

                if (ee.Count != 0) //Si la lista tiene elementos almacenados, le sumo el nuevo objeto con el IdEntidad obtenido
                {
                    tempee = NEntidadEncabezado.EntidadEncabezado(Convert.ToInt32(IdEntidad));
                }
                else //Si la lista no tiene elementos, creo una nueva lista 
                {
                    ee = new BindingList<EntidadEncabezado>(NEntidadEncabezado.EntidadEncabezado(Convert.ToInt32(IdEntidad)).ToList());
                }
                //Concateno las 2 BindingLists y las guardo en "ee"
                ee = new BindingList<EntidadEncabezado>(ee.Concat<EntidadEncabezado>(tempee).ToList());

                i++; // Actualizo el índice
            }
        }
        return ee;
    }  

When I debug the code I see that everything is fine, just that I do not see why the list is not generating me.
Clarifications

  • The class 'Account' has an Entity property, from which I get the Entity Id.
  • The Header Entity class has only the EntityName and EntityName properties.
  • I'm using Devexpress controls but the BindingList works the same as in WinForms.
  • asked by Pablo Matias 06.07.2017 в 14:29
    source

    1 answer

    1

    In all the passes you are doing

    ee = new BindingList<EntidadEncabezado>(NEntidadEncabezado.EntidadEncabezado(Convert.ToInt32(IdEntidad)).ToList());
    

    and

    ee = new BindingList<EntidadEncabezado>(ee.Concat<EntidadEncabezado>(tempee).ToList());
    

    So it seems that you are always creating a new list .. and only the last element created will be left. I do not understand much the logic of using two lists, nor do I know what procedures you call, but you should not add elements to the list instead of creating a binding list on each round?

        
    answered by 06.07.2017 в 15:36