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.”?