How to associate a GridView with a list of several types?

I get this error:

System.Reflection.TargetException: The object does not match the type of target.

when trying to bind List<IEvent>, where IEvent may be an appointment, a birthday, or several other events related to a calendar event.

+2
source share
3 answers

This cannot be done, do not use gridview, if you need this functionality, this is the only way to get around it.

-1
source

ChanChan, GridView , , concreate. ( Event) .

        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;
             }
         }

, "", " ". , , IEvent , - "". .

, , Valve.

+2

, , GridView - TypeDescriptionProvider .

Read more in this article: Polymorphic Data Binding Solutions

+1
source

Source: https://habr.com/ru/post/1709288/


All Articles