3

I am trying to bind a list in my namespace to BindingSource through designer. I know how to bind a list in code behind but I would also like to know if it is possible to do the same in designer.

Using the "Data Source Configuration Wizard", I have selected "Object" but it shows only namespace and classes.

enter image description here

I choose a class with List and clicked Finish. enter image description here

This is the code generated in designer after choosing the class name

this.bindingSource1.DataSource = typeof(Template.Form3);

It looks like windows forms does not have support for binding a list in designer. I am not sure this is the right method or windows forms does not support it. If windows forms have no support for binding to an object, can anyone explain the reason?

Edit: I tried the suggestion in answer to choose a data member, but it does not bind the binding source with actual data in List. Now designer code looks like

this.bindingSource1.DataMember = "data";
this.bindingSource1.DataSource = typeof(Template.Form3);
Kira
  • 1,403
  • 1
  • 17
  • 46

1 Answers1

4

The trick is to rebuild your solution, then any public class will be visible in the dropdown list to choose the datasource type from.

Then from the designer, click the binding source (bottom of screen) => Properties => DataMember => Select Property in your class to bind to (A List or Collection)

Edit:

Binding through the designer allows generation of columns at Design time, but it seems that you need to set the BindingSource's Data at runtime.

Since the bind object can't be static memeber but instance member.

private void bindingForm_Load(object sender, EventArgs e)
{
    myDataSourceBindingSource.DataSource = (new  myDataSource()).MyDataSourceList;
}

Designer:

// 
// colADataGridViewTextBoxColumn
// 
this.colADataGridViewTextBoxColumn.DataPropertyName = "ColA";
this.colADataGridViewTextBoxColumn.HeaderText = "ColA";
this.colADataGridViewTextBoxColumn.Name = "colADataGridViewTextBoxColumn";
// 
// myDataSourceBindingSource
// 
this.myDataSourceBindingSource.DataMember = "MyDataSourceList";
this.myDataSourceBindingSource.DataSource = typeof(myNameSpace.myDataSource);

Class:

public class myDataSource
{
    public BindingList<myData> MyDataSourceList
    {
        get
        {
            var list = new List<myData>()
            {
                new myData() { ColA = "A" },
                new myData() { ColA = "B" }
            };

            return new BindingList<myData>(list);
        }
    }
}

public class myData
{
    public string ColA { set; get; }
}

I don't know if this makes sense for you, but this is how it works in Windows Forms.

Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • Thanks for your answer, I tried your suggestion but data is not bound to the binding source. Now designer generated code looks like this.bindingSource.DataMember = "data"; this.bindingSource.DataSource = typeof(Template.Form3); – Kira May 19 '16 at 06:33
  • 1
    I can understand why it's not working. Data source is actually a type instead of an object. If it is an object, your suggestion will work. Since designer binds a type, I don't think it is possible to bind with an object in designer – Kira May 19 '16 at 06:47
  • Can you show you "Data" Property which holds the datasource and how you're adding data to it ? – Zein Makki May 19 '16 at 06:49
  • Data is a property of type BindingList in class Form3. For testing purpose I'm adding 10 data randomly. – Kira May 19 '16 at 06:54
  • Is it working for you? If yes, pls share your designer generated code in answer. – Kira May 19 '16 at 06:56
  • Thanks, I know this approach. So windows forms does not provide direct support. – Kira May 19 '16 at 07:07