如何将数组转换为Bindinglist?

时间:2014-09-04 07:23:18

标签: winforms linq entity-framework datagridview linq-to-entities

  • 你能解释一下为什么会出现这两种不同的输出吗?

Click here to see the image

  • 在描述中,我翻译了代码以更好地理解问题

  • 如何在IBindingList中转换数组?因为我的DataGridView

  • 是不可能的

代码:

public class ProductDALC
{
    public List<Product> ListProducts() //Product is the table name of my Database named DotNetStore
    {
        DotNetDataContext DataModel = new DotNetDataContext();
        var query = from objProduct in DataModel.Product.AsEnumerable()
                  select new Product { idProduct = objProducto.CodProduct, Name = objProduct.Name };

        return query.ToList();    
    }

    public Array ArrayProducts()
    {
        DotNetDataContext DataModel = new DotNetDataContext();
        var query = from objProduct in DataModel.Product.AsEnumerable()
                    select new
                    {     
                        idProduct = ("0000" + Convert.ToString(objProduct.idProduct)).Substring(("0000" + Convert.ToString(objProduct.idProduct)).Length - 4, 4),
                        Nombre = objProduct.Name,
                        Cantidad = objProduct.Quantity

                    };

        return query.ToArray();
    }
}

1 个答案:

答案 0 :(得分:0)

你试试这个:http://msdn.microsoft.com/en-us/library/ms132681.aspx

public BindingList<Product> ArrayProducts()
{
    DotNetDataContext DataModel = new DotNetDataContext();
    var query = from objProduct in DataModel.Product.AsEnumerable()
                select new Product
                {     
                    idProduct = ("0000" + Convert.ToString(objProduct.idProduct)).Substring(("0000" + Convert.ToString(objProduct.idProduct)).Length - 4, 4),
                    Nombre = objProduct.Name,
                    Cantidad = objProduct.Quantity 
                };

    return new BindingList<Customer>(query.ToList());
}