5

For the life of me I I don't seem to be able to get Databinding to Dynamics or ExpandoObjects working.

I have tried this in WinForms and in WebForms and get different results in each:

In ASP.NET:

<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>

    protected void Page_Load(object sender, EventArgs e)
    {
        dynamic contacts = new List<dynamic>();

        contacts.Add(new ExpandoObject());
        contacts[0].Name = "Patrick Hines";
        contacts[0].Phone = "206-555-0144";

        contacts.Add(new ExpandoObject());
        contacts[1].Name = "Ellen Adams";
        contacts[1].Phone = "206-555-0155";


        DropDownList1.DataSource = contacts;
        DropDownList1.DataTextField = "Name";
        DropDownList1.DataBind();

    }

This results in:

DataBinding: 'System.Dynamic.ExpandoObject' does not contain a property with the name 'Name'.

In WinForms, I have a different issue:

        dynamic contacts = new List<dynamic>();

        contacts.Add(new ExpandoObject());
        contacts[0].Name = "Patrick Hines";
        contacts[0].Phone = "206-555-0144";

        contacts.Add(new ExpandoObject());
        contacts[1].Name = "Ellen Adams";
        contacts[1].Phone = "206-555-0155";

        this.departmentList.DataSource = contacts;
        this.departmentList.DisplayMember = "Name";

This results in the ComboBox displaying "System.Dynamic.ExpandoObject" - as it is just calling ToString() on the two items in the collection. :(

I appreciate the help!

armasanea
  • 414
  • 2
  • 6
ProVega
  • 51
  • 2
  • 1
    I can't speak to those api's, Dynamic databinding does work in WPF though. – jbtule Sep 24 '11 at 02:40
  • 1
    Have you seen this question [How to databind a gridview to an ExpandoObject](http://stackoverflow.com/questions/4740969/how-to-databind-a-gridview-to-an-expandoobject) and [this](http://stackoverflow.com/questions/1653046/what-are-the-true-benefits-of-expandoobject/1653069#1653069) answer to a different question? – R0MANARMY Oct 10 '11 at 02:23
  • I don't get errors but in Windows Store apps, the initial value read is fine but the subsequent change notification doesn't seem to work. – Luke Puplett Mar 18 '14 at 16:49

1 Answers1

0

Try this

var contacts = new List<dynamic>()
{
    new {Name = "Patrick Hines",Phone = "206-555-0144"},
    new {Name = "Ellen Adams",Phone = "206-555-0155"}
};
Tunaki
  • 116,530
  • 39
  • 281
  • 370