How to bind a list which is a property of an object to DataGridView?

Mas Bagol

I have two object. One of them has a property which is a list of other object. For example, I have an object Klass that has students property that contains a list of Student objects. I need my DataGridView to display data of students of the selected Klass object which is selected via a ComboBox. So, whenever I change the selected Klass with ComboBox, the data DataGridView will display a list of students according to the selected Klass object.

The simplified code written like this:

Student class:

public class Student
{
    public string name { get; set; }
    public string address { get; set; }
}

Klass class:

public class Klass
{
    public string name { get; set; }
    public BindingList<Student> students { get; set; }
}

Is there anyway to do that?

Tim Eeckhaut

Set the combobox displaymember:

        public Form1()
        {
            InitializeComponent();

            comboBox1.DisplayMember = "name";
            comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
        }

Fill the combobox with Klasselements:

        private void Form1_Load(object sender, EventArgs e)
        {
            BindingList<Klass> klassList = new BindingList<Klass>(Util.CreateMockupData());
            comboBox1.DataSource = klassList;
        }

Implement handler to the combobox to set the datasource of the datagridview:

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var selectedKlass = comboBox1.SelectedItem as Klass;
            if (selectedKlass != null)
            {
                dataGridView1.DataSource = selectedKlass.students;
            }
        }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to bind List object with no duplicate property

From Dev

How to bind control to object property?

From Dev

How to bind control to object property?

From Dev

How to bind a list of objects of a class to a datagridview whose one field is again an object of some other class?

From Dev

issue to bind a a List<> to a DataGridView

From Dev

Datagridview: how to "typeconvert" a property from a list

From Dev

How to bind a Navigation Property (second level properties) in DataGridView using BindingSource?

From Dev

How to bind an object list with thymeleaf?

From Dev

How I can bind property, which use another static property

From Dev

Is it possible to bind a List<Object> which contains List<String> to DataGrid

From Dev

How to bind property on parent object with knockoutjs?

From Dev

How to bind template to object in array by property value?

From Dev

How to parse a json object property to bind it to a control

From Dev

How to bind to specific property in list box

From Dev

How to bind to specific property in list box

From Dev

Winforms How to Bind to a List<long> DatagridView and have it show correctly

From Dev

How to bind List<dynamic> to datagridview in c# windows form

From Dev

Bind Data in List<T> to DatagridView

From Dev

Bind Data in List<T> to DatagridView

From Dev

How to bind a property to a singleton object property from QML

From Dev

List object ID value bind with dialog modal property

From Dev

How to bind text property of two TextFields which are in different FXMLs

From Dev

How to bind ListBox ItemsSource to a ViewModel property (which is a Collection) programmatically?

From Dev

How to bind to a control's property which is inside control template?

From Dev

Bind to property of object

From Dev

It is legal to exec private function in property setter which is bind to xaml object property?

From Dev

How to bind a list of custom object as source to listbox?

From Dev

Binding a DataGridView to a Object list

From Dev

Binding a DataGridView to a Object list

Related Related

  1. 1

    How to bind List object with no duplicate property

  2. 2

    How to bind control to object property?

  3. 3

    How to bind control to object property?

  4. 4

    How to bind a list of objects of a class to a datagridview whose one field is again an object of some other class?

  5. 5

    issue to bind a a List<> to a DataGridView

  6. 6

    Datagridview: how to "typeconvert" a property from a list

  7. 7

    How to bind a Navigation Property (second level properties) in DataGridView using BindingSource?

  8. 8

    How to bind an object list with thymeleaf?

  9. 9

    How I can bind property, which use another static property

  10. 10

    Is it possible to bind a List<Object> which contains List<String> to DataGrid

  11. 11

    How to bind property on parent object with knockoutjs?

  12. 12

    How to bind template to object in array by property value?

  13. 13

    How to parse a json object property to bind it to a control

  14. 14

    How to bind to specific property in list box

  15. 15

    How to bind to specific property in list box

  16. 16

    Winforms How to Bind to a List<long> DatagridView and have it show correctly

  17. 17

    How to bind List<dynamic> to datagridview in c# windows form

  18. 18

    Bind Data in List<T> to DatagridView

  19. 19

    Bind Data in List<T> to DatagridView

  20. 20

    How to bind a property to a singleton object property from QML

  21. 21

    List object ID value bind with dialog modal property

  22. 22

    How to bind text property of two TextFields which are in different FXMLs

  23. 23

    How to bind ListBox ItemsSource to a ViewModel property (which is a Collection) programmatically?

  24. 24

    How to bind to a control's property which is inside control template?

  25. 25

    Bind to property of object

  26. 26

    It is legal to exec private function in property setter which is bind to xaml object property?

  27. 27

    How to bind a list of custom object as source to listbox?

  28. 28

    Binding a DataGridView to a Object list

  29. 29

    Binding a DataGridView to a Object list

HotTag

Archive