Link to home
Start Free TrialLog in
Avatar of myebay
myebay

asked on

how to fill a datagrid with list<object>

Hello,
I have a list of Users objects (list<user>) and I want to display them in a datagridview. I don't know how to do it?
 
Thanks for your help
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Did you add columns to the DataGridView from the designer.  You are going to need to set DataPropertyName for each column to properties on the User object.  And, they need to be properties, and not fields.
Avatar of myebay
myebay

ASKER

I want to do all by code, not by design.
Actually, the same process applies either by code or by designer--set DataPropertyName to a property in the class instance that you want to display from, and then set DataGridView DataSource to the list.
Avatar of myebay

ASKER

do you have an example ?
Something like this:

            DataGridViewTextBoxColumn column1 = new DataGridViewTextBoxColumn();
            column1.DataPropertyName = "FirstName";
            this.dataGridView1.Columns.Add(column1);
Avatar of myebay

ASKER

I've tried this :

this.userGrid.DataSource = Controler.UserManager.Instance.GetUsers(); // it's a list of users
DataGridViewTextBoxColumn column1 = new DataGridViewTextBoxColumn();
column1.DataPropertyName = "Name";
this.userGrid.Columns.Add(column1);

But it doesn't work : the object reference is not define to an instance of an object.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of myebay

ASKER

It seems that my error comes from the connection :

I got an exception when I do that
DbProviderFactory factory = DbProviderFactories.GetFactory(CnxStringSettings.ProviderName);

I really don't know what is wrong :(
You might be having a problem with 'CnxStringSettings.ProviderName'.
Avatar of myebay

ASKER

thank you