views:

873

answers:

2

I need to populate a .net DataGridView from a collection physically (without data binding) in C#. That is, I should use foreach loops and iterate through the collection while creating a row for each object and a cell for each property.

I have created the columns in design mode. Now I need to create rows dynamically.

How to do that?

A: 

Add an array of objects, the number of which should correspond to the number of columns.

e.g.

this.datagridview1.Rows.Add(new object[] { "col1", "col2", "col3", 4.5, true});

Ian
Thanks @Lan!
A: 

Check this --> http://shahed-kazi.blogspot.com/2009/03/bind-gridview-to-dynamically-created.html

Bhaskar
I don't want to use data binding feature. How to do it by using foreach?
Check the link...
Bhaskar
You can create a data-table out of your data, loop trough your existing data collecytion and add rows to your data table and bind that data table into your GridView
Bhaskar
Thanks a lot @Bhaskardeep !