Reputation: 3
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>
Upvotes: 0
Views: 1249
Reputation: 7573
The ItemsSource is not null, it's just that ArrayList
does not implement IEnumerable<testClass>
, and therefore the cast you perform returns null
. If you use
var list = (IEnumerable<testClass>) datagrid.ItemsSource;
you will get an error saying that this cast is not valid.
If you use a List<testClass>
instead of ArrayList
for 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 ArrayList
or IEnumerable (non-generic)
instead, if you wish to have an interface.
Upvotes: 1