[ACCEPTED]-Bind List of Classes into DataGridView-datagridview

Accepted answer
Score: 20

How about binding to an anonymous type:

public void BindClass() 
{
    dataGridView1.DataSource = myList.Select(myClass => new {myClass.Data.ID, myClass.Data.Name}).ToList();
}

Will 2 you be updating the data in the datagridview 1 ?

Score: 2

To do that without changing the model is 3 exceptionally tricky (but possible), requiring ICustomTypeDescriptor or TypeDescriptionProvider, and 2 a custom PropertyDescriptor. To be honest: not worth it.

Just 1 add pass-thru properties:

public MyClass
{
   public MyDataClass Data{get; set;}
   [DisplayName("ID")]
   public string DataID {
     get {return Data.ID;}
     set {Data.ID = value;}
   }
   [DisplayName("Name")]
   public string DataName {
     get {return Data.Name;}
     set {Data.Name = value;}
   }
}
Score: 1

It's easy with LINQ as you can see in This answer

Here's 2 a simple implementation of something I needed 1 to attach to datagridview.

     DataGridView1.DataSource =  _
(From i In ItemList Select i.ListID, i.FullName, i.PurchaseDesc, i.EditSequence).ToList
Score: 0

No, you can't do this out of the box. You 4 will have to write a custom binding source 3 (most likely with specialized logic for 2 your specific purpose) to allow 'drilling' deeper 1 than just 1 level of properties.

More Related questions