I have a named class
public class testClass
{
public testClass(string showCode, string urn)
{
ShowCode = showCode;
URN = urn;
}
public string ShowCode { get; set; }
public string URN { get; set; }
}
I create an ArrayList, add to the list and bind it to a wpf datagrid
ArrayList l = new ArrayList();
l.Add(new testClass("ose11", "7016463"));
this.grdTestData.ItemsSource = l;
This displays just what I want in the datagrid.
Now I want to get the datagrid’s data back and iterate throught it
IEnumerable<testClass> t = this.grdTestData.ItemsSource as IEnumerable<testClass>;
..but ‘t‘ is null! !! this is the problem !!
This is the datagrid definition:
<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="12,66,0,48" Name="grdTestData" Width="200" CanUserAddRows="True" >
<DataGrid.Columns>
<DataGridTextColumn Header="ShowCode" Binding="{Binding ShowCode}" />
<DataGridTextColumn Header="URN" Binding="{Binding Path=URN}" />
</DataGrid.Columns>
</DataGrid>
The ItemsSource is not null, it’s just that
ArrayListdoes not implementIEnumerable<testClass>, and therefore the cast you perform returnsnull. If you useyou will get an error saying that this cast is not valid.
If you use a
List<testClass>instead ofArrayListfor the source, the cast will be valid and not return null.If you don’t wish to use a generic collection, then cast it to
ArrayListorIEnumerable (non-generic)instead, if you wish to have an interface.