开发者

System.Windows.Forms.DataGridView does not display data

开发者 https://www.devze.com 2022-12-30 22:25 出处:网络
All I am doing is a simple: // Both the methods, order.GetAllOrderItems() and order.GetOrderedItemsWhereBrandIs(\"foo\")

All I am doing is a simple:

    // Both the methods, order.GetAllOrderItems() and order.GetOrderedItemsWhereBrandIs("foo")
    // return an IEnumerable<T> so the assignment to the DataSource property of the DataGridView
    // should be fine. The problem is in re-assigning the data source property.
    public void DisplayItems()
    {
        // The data appears if I have just this line.
        dgvOrderedItems.DataSource = order.GetAllOrderItems();

        dgvOrderedItems.DataSource = null;

        // This time the data grid does not take the new data source. Instead, because
        // of the null assignment in the previous statement, it displays no data at all.
        dgvOrderedItems.DataSource = order.GetOrderedItemsWhereBrandIs("Lenovo");
    }

My question is: is there a way to change the data source of a D开发者_开发技巧ataGridView control once it has been set? I am using C# 4.0 and Visual Studio 2010 for development.


Databinding cannot be used with IEnumerables; you can only bind to an IList or better.

Add .ToArray() to turn the IEnumerable into an IList<T>.

The reason it works the first time is probably because your GetAllOrderItems doesn't perform any LINQ calls and ends up returning an object that implements IList.

However, since your GetOrderedItemsWhereBrandIs method (presumably) includes a Where() call, it returns an object that only implements IEnumerable.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号