none
Bind Dynamically created controls. RRS feed

  • Question

  • Hi friends,

    I am using Visual Studio 2010 to develop a windows application.

    In one of the scenario I have to create labels at runtime and bind them with the property of the object so that whenever the property of the object will change that should reflect to the label.
    The problem I am facing is at the first time when I create the labels and bind them, labels show the value that are currently assigned to the object property. but after that when the property of the binded object changed it does not update the label. I am also giving my code that I have used to ind and create the labels. please suggest me the correct way of doing this or if I am doing something wrong.

                Label lblValue;
                Label lblDel;

                flpControls.Controls.Clear();
                flpLabels.Controls.Clear();        //flpControls & flpLabelsare FlowLayoutPanels.

                foreach (MyClass MyObject in MyObjectList)
                {
                    ObjUserControl = new MyUserControl(MyObject);
                    flpControls.Controls.Add(ObjUserControl);
                    lblValue = new Label();
                    lblValue.DataBindings.Add(new Binding("Text", MyObject.Object2, "Text"));  //Here I want to bind MyObject.Object2.Text property to lblValue.
                    flpLabels.Controls.Add(lblValue);
                    lblDel = new Label();
                    lblDel.DataBindings.Add(new Binding("Text", TagCode, "Text"));    //Here I want to bind TagCode.Text property to lblValue.
                    flpLabels.Controls.Add(lblDel);
                }

    Please go thrugh the code and help me to fix this.

    Regards,
    Girish

    Wednesday, April 10, 2013 5:51 AM