19 December 2013

Bind the "List" as DataSource to the DataGridView

Hi Friends,

Sometime we encounter the problem when we have List<MyClass> list and we want to bind it with DataGrid or DataGridView.  Of course we can bind it by using the following code.

Lets see how we can bind the List to the DataGridView

            // create the list for the objects of "MyClass"
            List<MyClass> list = new List<MyClass>();
            ..
            ..// add the object of "MyClass" into the "list".
            ..
            // create the binding list from the "List list" object
            var bindingList = new BindingList<MyClass>(list);

            // create the BindginSource from the "bindingList" variable
            BindingSource bindingSource = new BindingSource(bindingList, null);

            // bind the DataSource to the DataGridView object "dataGridView1"
            dataGridView1.DataSource = bindingSource;

The above code will work perfectly and you can bind the List in this way as DataSouce for the DataGridView.

Happy Knowledge Sharing.. :)

No comments:

Post a Comment