Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connection String 16 61
SqlDependency to get update from sql to my c# app 2 42
tableview is not updating 1 11
VB.NET 2008 Winforms Signing 13 20
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

832 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