Solved

assigning array of objects to datasource of a datagridview (want object properties to become the columns)

Posted on 2006-06-15
2
728 Views
Last Modified: 2010-05-18
hi !

I have code like:


dataGridView1.DataSource = FunctionWhichReturnsArrayOfCustomObject();



I want this array of objects to be displayed in the datagridview, with the public properties becoming the columns. However, it isnt working. Is there anything additional i need to do?


Thanks :)
muskad202
0
Comment
Question by:muskad202
2 Comments
 
LVL 2

Accepted Solution

by:
Anandavally_P earned 25 total points
ID: 16910186
Try this sample code:

This is the Person class with public properties :

Person.cs
-----------
class Person
    {
        private String name;
        private int age;

        public String Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                age = value;
            }
        }
    }
This is the form class with the datagrid component:

Form1.cs
----------
// Function to load the objects in the array
    private Array FunctionWhichReturnsArrayOfCustomObject()
        {
            Array PersonObj = new Person[2];

            Person p1 = new Person();
            Person p2 = new Person();

            p1.Name = "Michael";
            p1.Age = 62;

            p2.Name = "John";
            p2.Age = 58;

            PersonObj.SetValue(p1, 0);
            PersonObj.SetValue(p2, 1);

            return PersonObj;
   }  
// Function to display the objects in the datagrid
private void Form1_Load(object sender, EventArgs e)
   {
            try
            {
                Array GetPersons = FunctionWhichReturnsArrayOfCustomObject();
                dataGridView1.DataSource = GetPersons;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception :" + ex.ToString());
            }
           
  }

I think this could help you

Thanks
Anandavally P


   
   
0
 
LVL 2

Author Comment

by:muskad202
ID: 16910256
damn ... i realized my careless mistake ... i didnt have any public properties, just public fields !!!

thanks :)
muskad202
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Data Saving 5 56
SQL Exceptions 3 36
Reset asp.net login password 4 26
WCF Service Application cannot connect from TCP terminal 1 11
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now