BidingSource at runtime

G

Gian Paolo

hi all
about biding source i saw that the ide, if you use the object from toolbox,
allows you to create a dataset a tableadatpter and a bidingource.
Then for example connect this biding source to a textbox.
After that you can draw a datagridview on the form (at design time) and
create a new bidingsource and set the datasource property of the second
binding source the first bindig source and like datamenber something like
Tableofthefirstbidingsource_tableofthesecondbidingosurce now i can do all
these things at runtime the only thing i do not undderstand is how to set is
the data member of the second bindig source.
Someone know how to do ?
 
M

Marc Gravell

Example - this do? (note only Susan has sub-data here...)

class Person
{
private readonly string _name;
public string Name {get {return _name;}}
private readonly DateTime _dob;
public DateTime DateOfBirth {get {return _dob;}}
private readonly List<Person> _kids = new List<Person>();
public IList<Person> Children { get { return _kids; } }
public Person(string name, DateTime dob)
{
_name = name;
_dob = dob;
}
}
static void Main()
{
List<Person> people = new List<Person>();
people.Add(new Person("Fred",new DateTime(1970,1,1)));
people.Add(new Person("Jo",new DateTime(1971,2,2)));
Person susan = new Person("Susan", new DateTime(1972, 3, 3));
people.Add(susan);
susan.Children.Add(new Person("Tony", new DateTime(1990, 4,4)));
susan.Children.Add(new Person("Pippa", new DateTime(1991, 5, 5)));

using(Form form = new Form())
using(BindingSource parents = new BindingSource())
using (BindingSource children = new BindingSource())
using (ListBox parentsListBox = new ListBox())
using (ListBox kidsListBox = new ListBox())
{
form.Text = "Binding demo";
parents.DataSource = typeof(Person);
children.DataSource = parents;
children.DataMember = "Children";
parentsListBox.Dock = DockStyle.Left;
parentsListBox.DataSource = parents;
parentsListBox.DisplayMember = "Name";
kidsListBox.DataSource = children;
kidsListBox.DisplayMember = "Name";
kidsListBox.Dock = DockStyle.Right;
form.Controls.AddRange(new Control[] { kidsListBox,
parentsListBox });
parents.DataSource = people;
form.ShowDialog();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top