Click here to Skip to main content
15,888,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following runs, but the elements of array[] (in MyObject) don't get displayed in the dataGridView control. Only Checked and Signalname get displayed.
Adding members to MyObject and mapping them to the individual elements of array[] via get and set accessors does the trick, but there's got to be a better way.
C#
    public partial class Form1 : Form
    {
        MyObject[] myArray;
        
        public Form1()
        {
            InitializeComponent();

            myArray = new MyObject[] 
            {
                new MyObject(true, "Signal one"),
                new MyObject(false, "/Signal two"),
                new MyObject(true, "Signal 3"),
                new MyObject(false, "/Signal 4")
            };
        }

        private void button2_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = myArray;
        }
    }

    public class MyObject
    {
        public bool Checked { get; set; }
        public string Signalname { get; set; }
        public int[] array;

        public MyObject(bool Ckd, string Signame)
        {
            Checked = Ckd;
            Signalname = Signame;
            array = new int[2] { 111, 222};
        }
    }
}


What I have tried:

Adding members to MyObject and mapping them to the individual elements of array[] via get and set accessors does the trick, but there's got to be a better way.
Posted
Updated 15-Nov-16 6:02am

1 solution

When you databind an object, only properties are displayed, not fields, even if they are public. It you want the array elements displayed, each of them needs to have an individual property which returns a specific element - you can't bind to a whole collection to create multiple columns, because the collection is an object: all that would be displayed is the name of the collection, such as "System.Int32[]".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900