Hi all,
I have a C# web app where I have a gridview that takes a List<> that takes an interface. There are three object types that implement the interface that are within the list.
I set the gridviews datasource to the list and call databind. When I run the app I get the following error:
{"Property accessor 'Name' on object 'Bailey.Objects.Company' threw the following exception:'Object does not match target type.'"}
I changed the object order around in the list and found that the first object type works just fine but when it finds the first instance of another object it throws this error.
List<IBaileyObject> listToDisplay = listofItems;
GridViewItemList.DataSource = listToDisplay;
GridViewItemList.DataBind();
What I wanted was for the datasource and binding to stay on the interface rather then cast down to the object as it would seem to be happening.
Is this because the List<> is returning the object as it self rather than the Interface or is it something in the gridview that is casting it down?
Or am I going down the wrong track?
I hope someone has a way to fix this please.
Thanks
Jon Hawkins
EDIT
<asp:TemplateField HeaderText="Selected">
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkbox" Width="10%" Enabled="true" AutoPostBack="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID" Visible="False" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="TypeName" HeaderText="Type" />
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#E3EAEB" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="#F8F8F8" />
The interface exposes Name, ID and TypeName.
" HeaderText="ID" Visible="False" />
gives can not apply indexer.