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

Posted on 2006-06-15
Medium Priority
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 :)
Question by:muskad202
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

Anandavally_P earned 100 total points
ID: 16910186
Try this sample code:

This is the Person class with public properties :

class Person
        private String name;
        private int age;

        public String Name
                return name;
                name = value;

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

// 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)
                Array GetPersons = FunctionWhichReturnsArrayOfCustomObject();
                dataGridView1.DataSource = GetPersons;
            catch (Exception ex)
                MessageBox.Show("Exception :" + ex.ToString());

I think this could help you

Anandavally P


Author Comment

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

thanks :)

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

741 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