2008-12-02 52 views

回答

-1

這是無法完成的,如果您需要此功能,請不要使用gridview,這是我可以解決它的唯一方法。

+0

這是可以做到,有幾種解決方法:使用繼承,而不是接口,使用供應商,使用自定義綁定列(http://iridescence.no/post/FixingBoundFieldSupportforCompositeObjects.aspx )... – 2010-01-06 16:00:00

2

ChanChan, GridView控件不支持綁定到的接口的集合, 嘗試redisigning您的應用程序,以確保綁定只concreate類的集合。 唯一的解決方法是使用基類(在你的情況下事件)並綁定到事件集合。

 public class Event 
     { 
      public DateTime StartDate { get; set; } 
     } 
     public class Birthday : Event 
     {  

      public DateTime? EndDate { get; set; } 
     } 
     public class Appointment : Event 
     {  

      public string Place { get; set; } 
     } 
     public class EventCollection : Collection<Event> 
     { 
      public static EventCollection GetEvents() 
      { 
       var events = new EventCollection(); 
       events.Add(new Birthday 
       { 
        EndDate = DateTime.Now.AddDays(1), 
        StartDate = DateTime.Now 
       }); 
       events.Add(new Appointment 
       { 
        Place = "Gallery", 
        StartDate = DateTime.Now 
       }); 
       return events; 
      } 
     } 

請注意,從基類:繼承創造「是」的關係,但是從接口繼承 創建「能做」的關係。換句話說,IMO,實施IEvent 將是一個糟糕的設計,因爲生日'是'事件。我會在這裏繼承基類。

希望這會有所幫助, Valve。

1

這樣做有點痛苦,但可以將多種類型的列表綁定到GridView - 您只需爲您的基本類型實現TypeDescriptionProvider即可。

這篇文章中的更多細節:Polymorphic Databinding Solutions

相關問題