Lista jako DataSet

0

Cześć wszystkim.

Mam pewien problem z konwersja listy na DataSet.
Używam takiego kodu do konwersji listy na ds:

public static void AddToDataSet(DataSet set, object value)
        {
            if (set == null)
                throw new ArgumentNullException(nameof(set));

            if (value == null)
                return;

            var type = value.GetType();
            var table = set.Tables[type.FullName];
            if (table == null)
            {
                table = new DataTable(type.FullName);
                set.Tables.Add(table);
                foreach (var prop in type.GetProperties().Where(p => p.CanRead))
                {
                    if (IsEnumerable(prop))
                        continue;

                    var col = new DataColumn(prop.Name, prop.PropertyType);
                    table.Columns.Add(col);
                }
            }

            var row = table.NewRow();
            foreach (var prop in type.GetProperties().Where(p => p.CanRead))
            {
                object propValue = prop.GetValue(value);
                if (IsEnumerable(prop))
                {
                    if (propValue != null)
                    {
                        foreach (var child in (ICollection)propValue)
                        {
                            AddToDataSet(set, child);
                        }
                    }
                    continue;
                }

                row[prop.Name] = propValue;
            }
            table.Rows.Add(row);
        }

        private static bool IsEnumerable(PropertyInfo pi)
        { 
            return typeof(ICollection).IsAssignableFrom(pi.PropertyType);
        }

lista wygląda tak:

using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using DevExpress.DataAccess.ObjectBinding;

namespace INTEGRA
{
    [DisplayName("Rolety")]
    [HighlightedClass]
    public class roletaClass : List<roletaClass>
    {
        int lp;
        decimal szeroko;
        decimal wysoko;
        int ilosc;
        decimal cena;
        decimal wartN;
        decimal wartB;

        public int RLp
        {
            get { return lp; }
            set { lp = value; }
        }
        public decimal RSzerokość
        {
            get { return szeroko; }
            set { szeroko = value; }
        }
        public decimal RWysokość
        {
            get { return wysoko; }
            set { wysoko = value; }
        }
        public int RIlość
        {
            get { return ilosc; }
            set { ilosc = value; }
        }
        public decimal RCena
        {
            get { return cena; }
            set { cena = value; }
        }
        public decimal RWartN
        {
            get { return wartN; }
            set { wartN = value; }
        }
        public decimal RWartB
        {
            get { return wartB; }
            set { wartB = value; }
        }
    }
}

Może podpowie ktoś co robię źle, że za każdym razem kiedy w liście znajdują się jakieś wartości wywala mi „Niezgodność liczby parametrów.”?

0

moze to pomoze

https://stackoverflow.com/questions/1245662/convert-generic-list-to-dataset-in-c-sharp

 public static DataSet ToDataSet<T>(this IList<T> list)
    {
        Type elementType = typeof(T);
        DataSet ds = new DataSet();
        DataTable t = new DataTable();
        ds.Tables.Add(t);

        //add a column to table for each public property on T
        foreach (var propInfo in elementType.GetProperties())
        {
            Type ColType = Nullable.GetUnderlyingType(propInfo.PropertyType) ?? propInfo.PropertyType;

            t.Columns.Add(propInfo.Name, ColType);
        }

        //go through each property on T and add each value to the table
        foreach (T item in list)
        {
            DataRow row = t.NewRow();

            foreach (var propInfo in elementType.GetProperties())
            {
                row[propInfo.Name] = propInfo.GetValue(item, null) ?? DBNull.Value;
            }

            t.Rows.Add(row);
        }

        return ds;
    }

1 użytkowników online, w tym zalogowanych: 0, gości: 1