views:

827

answers:

2

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.

+1  A: 

Can you post some of your GridView code (specifically where you reference your IBaileyObject properties)?

Not knowing what syntax you're using makes things hard, but have you tried doing something like this:

<%# (((IBaileyObject)Container.DataItem)["PropertyX"]) %>

Hope this helps

Edit after code posted

I think it's because you're using "BoundField"s - they're not clever enough to work out where the member you're referring to comes from. My advice is to convert your BoundFields to databinding syntax:

Either: <%# Eval("Id") %> or, <%# (((IBaileyObject)Container.DataItem)["Id"]) %>

Failing that, follow the advice in this blog post and create a custom bound field control.

Paul Suart
+1  A: 

I really like this solution : http://iridescence.no/post/FixingBoundFieldSupportforCompositeObjects.aspx

It uses a custom BoundField class that uses the Eval to return the values. A little nicer than using the Evals in the ASPX page...

Julien N