title: Make gridview datasource stay in interface not cast down to object
body: <p>I have a C# web app where I have a gridview that takes a List&lt;> that takes an interface.  There are three object types that implement the interface that are within the list.</p>
  
  <p>I set the gridviews datasource to the list and call databind. When I run the app I get the following error:</p>
  
  <pre><code>{"Property accessor 'Name' on object 'Bailey.Objects.Company' threw the following exception:'Object does not match target type.'"}
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>List&lt;IBaileyObject&gt; listToDisplay = listofItems;
  GridViewItemList.DataSource = listToDisplay;
  GridViewItemList.DataBind();
  </code></pre>
  
  <p>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.</p>
  
  <p>Is this because the List&lt;> is returning the object as it self rather than the Interface or is it something in the gridview that is casting it down?</p>
  
  <p>Or am I going down the wrong track?</p>
  
  <p>I hope someone has a way to fix this please.</p>
  
  <p>Thanks</p>
  
  <p>Jon Hawkins</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>
      </p>
  
  <pre><code>     &lt;asp:TemplateField HeaderText="Selected"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:CheckBox runat="server" ID="chkbox" Width="10%" Enabled="true" AutoPostBack="false" /&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
  
      &lt;asp:BoundField DataField="ID" HeaderText="ID" Visible="False" /&gt;
      &lt;asp:BoundField DataField="Name" HeaderText="Name" /&gt;
      &lt;asp:BoundField DataField="TypeName" HeaderText="Type" /&gt;
  &lt;/Columns&gt;
  &lt;FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /&gt;
  &lt;RowStyle BackColor="#E3EAEB" /&gt;    
  
  &lt;PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /&gt;
  &lt;SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /&gt;
  &lt;HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /&gt;
  &lt;EditRowStyle BackColor="#7C6F57" /&gt;
  &lt;AlternatingRowStyle BackColor="#F8F8F8" /&gt;    
  </code></pre>
  
  <p> </p>
  
  <p>The interface exposes Name, ID and TypeName.</p>
  
  <p>" HeaderText="ID" Visible="False" /></p>
  
  <p>gives can not apply indexer.</p>