Solved

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

Posted on 2006-06-15
2
733 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
[X]
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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

691 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