简体   繁体   中英

How to bind a list of objects of a class to a datagridview whose one field is again an object of some other class?

I have following code in which i have to bind a list of objects of a class to a datagridview. In this class one field is an object of some other class which again have some fields in it.Please refer the code.

public class Test
{
    public string m_field1_Test { get; set; }
    public string m_field2_Test { get; set; }
    public string m_field3_Test { get; set; }
    public string m_field4_Test { get; set; }
    .
    .
    public string m_field100_Test{get; set;}
    public Test1 test1obj;

    public Test()
    {
        m_field1_Test = "field1";
        m_field2_Test = "field2";
        m_field3_Test = "field3";
        m_field4_Test = "field4";
        .
        .
        m_field100_Test = "field100";

        test1obj = new Test1();
    }
}

public class Test1
{
    public string m_field1_Test1;
    public string m_field2_Test1;
    public string m_field3_Test1; 
    .
    .
    .
    public string m_field30_Test1;   

    public Test1()
    {

        m_field1_Test1 = "field1_Test1";
        m_field2_Test1 = "field2_Test1";
        m_field3_Test1 = "field3_Test1"; 
        .
        .
        .
        m_field30_Test1 = "field30_Test1";
    }       
}

On a Button click i am binding a list of Test Objects to dataGridView. Code is as follows.

   private void Button2_Click(object sender, EventArgs e)
    {
        List<Test> listTest = new List<Test>();
        Test obj;
        for (int i = 0; i < 1000000; i++)
        {
            obj = new Test();
            listTest.Add(obj);
        }
        label1.Text = listTest.Count().ToString();
        dataGridView1.DataSource = listTest;
        dataGridView1.BackgroundColor = Color.Beige;                
    }

When i execute this much part only field1 to field4 are appearing in the datagridview. How can i bind it in such a way so that Test1 fields also appear in datagridview in format.I want output something like this.

     field1 | field2  | field3| ....field100|field1_Test1 |field2_Test1|...field30_Test1
            |         |       |             |              |            |
            |         |       |             |              |            |
            |         |       |             |              |            |

Can someone please tell me how to implement this functionality??

Transforming your listTest to an anonymous object before you bind will expose the fields.

var dataSource = listTest.Select(l => new {field1 = l.m_field1_Test, 
                                           field2 = l.m_field2_Test, 
                                           field3 = l.m_field3_Test, 
                                           field4 = l.m_field4_Test,
                                           field1_Test1 = l.test1obj.m_field1_Test1,
                                           field2_Test1 = l.test1obj.m_field2_Test1, 
                                           field3_Test1 = l.test1obj.m_field3_Test1}); 

This would be better accomplished in your loop to 1000000, but I figured that looping to 1000000 is not something you would do in production.

If you wanted to eliminate the for loop, you could do the same with Enumerable.Range :

var dataSource = Enumerable.Range(0, 1000000).Select(...

Also, just a tip, you should avoid putting logic into your event handlers. Pull it into its own method so it can be reused in other places.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM