Question: I have a selectedItemCollection held within an object that holds information on the selected rows I have chosen from a bound datagrid. 

I am trying to use the information held within the selected datarows but I cannot get this to work. 

I have the tried the code below, but having no luck whatsoever, is there an easy way to iterate through an object that holds this information? 

Thanks for your help as always.

```
    public void createRpt(string reportNum, Object selectedItems)
    {
        //Find the information held within selectedItems and do something with it.
        foreach(var item in selectedItems as ItemCollection)  //This returns a 'null exeption on itemCollection' error.
        {
          //Do something with items
        }
    }

```

Answer: 
Casting to `IList` should work for you:

```
public void createRpt(string reportNum, Object selectedItems)
{
    var items = (System.Collections.IList)selectedItems;
    //var typedItems = items.Cast<YourCustomType>();
    foreach(var item in items)
    {
      //Do something with items
    }
}

```
To check why this error is coming and from where it is coming, you can change your method to below and *debug* it:

```
public void createRpt(string reportNum, Object selectedItems)
{
    //Add debugging code
    var itemCollection = selectedItems as ItemCollection;
    if(itemCollection == null)
    {
        Debug.WriteLine("selectedItems can not be casted to ItemCollection, or not initialized.");
        return;
    }

    if(itemCollection.Count <= 0)
    {
        Debug.WriteLine("Collection is empty");
        return;
    }

    //Find the information held within selectedItems and do something with it.
    foreach(var item in itemCollection) 
    {
      //Do something with items
    }
}

```