Solved

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

Posted on 2006-06-15
2
727 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

760 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

17 Experts available now in Live!

Get 1:1 Help Now