System.Windows.Forms.DataGridView does not display data

Everything I do is 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 the DataGridView control after it has been installed? I am using C # 4.0 and Visual Studio 2010 for development.

+2


a source to share


1 answer


Data binding cannot be used with IEnumerable

s; you can only attach to IList

or better.

Add .ToArray()

to turn IEnumerable

into IList<T>

.



The reason it works the first time is probably because yours is GetAllOrderItems

not making any LINQ calls and ends up returning the object that it implements IList

.

However, since your method GetOrderedItemsWhereBrandIs

(presumably) includes a call Where()

, it returns an object that only implements IEnumerable

.

+4


a source







All Articles