DataGridView with row-specific DataGridViewComboBoxColumn contents

Posted by XXXXX on Stack Overflow See other posts from Stack Overflow or by XXXXX
Published on 2009-09-01T12:24:38Z Indexed on 2010/05/04 1:38 UTC
Read the original article Hit count: 243

So I have something like the following data structure (constructors omitted)

class Child {
    public string Name { get; set; }
    public int Age { get; set; }
}

class Parent {
    public string Name { get; set; }
    public List <Child> Children { get; private set; }    // never null; list never empty
    public Child FavoriteChild { get; set; }              // never null; always a reference to a Child in Children
}

List < Parent > Parents;

What I want to do is show a DataGridView where each row is a Parent from Parent list. Each row should have two columns: a text box showing the parent's name and a DataGridViewComboBoxColumn containing that parent's children, from which the user can select the parent's favorite child.

I suppose I could do the whole thing manually, but I'd like to do all this with more-or-less standard Data Binding. It's easy enough to bind the DataGridView to the list of parents, and easy enough to bind the selected child to the FavoriteChild property.

The part that's giving me difficulty is that it looks like the Combo Box column wants to bind to one data source for all the combo-box's contents on all rows. I'd like each instance of the combo box to bind to the list of each parent's children.

I'm fairly new to C#/Windows Forms, so I may well be missing something obvious. Or it could be that "you can't get there from here."

It's not too tough to make a separate list of all the children and filter it by parent; I'm looking into that possibility right now. Is this feasible, or is there a better way?

© Stack Overflow or respective owner

Related posts about c#

Related posts about winforms