This is a migrated thread and some comments may be shown as answers.

Dynamic table creation

0 Answers 97 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 2
Andrew asked on 11 Nov 2011, 07:31 PM
I guess I'm just lost as to how I can do this if it is even possible. I have my poco that I would like to bind to the datasource of the table. But I can't figure out how to create the table/crosstable layout to look like the attached image. The problem I'm facing is that I may have n approaches with each approach having n lengths. All approaches have the same number of lengths. Example, I have five approaches with 4 lengths each. From there I have a count for each hour for each approach/length.

Any help on how I can dynamically create the table?

Robert

Here is my model:

public class CrosstabClassificationModel
    {
        public class ApproachModel
        {
            public ApproachModel(List<int> lengthGroups)
            {
                Lengths = new List<LengthModel>();
                int prev = 0;
                foreach(int i in lengthGroups)
                {
                    Lengths.Add(new LengthModel()
                    {
                        Min = prev,
                        Max = i
                    });
 
                    prev = i + 1;
                }
            }
 
            public string Name
            {
                get;
                set;
            }
 
            public ApproachTypes ApproachType
            {
                get;
                set;
            }
 
            public List<LengthModel> Lengths
            {
                get;
                private set;
            }
 
            public void Add(int length, DateTime time)
            {
                foreach(var l in Lengths)
                {
                    if(length.IsBetween(l.Min, l.Max))
                    {
                        l.Add(time);
                        break;
                    }
                }
            }
 
            public override string ToString()
            {
                return Name;
            }
        }
 
        public class LengthModel
        {
            public LengthModel()
            {
                Events = new List<EventModel>();
            }
 
            public string Title
            {
                get
                {
                    if(Max == int.MaxValue)
                        return string.Format("{0} and greater", Min);
                    else
                        return string.Format("{0} - {1}'", Min, Max);
                }
            }
 
            internal int Min
            {
                get;
                set;
            }
 
            internal int Max
            {
                get;
                set;
            }
 
            public List<EventModel> Events
            {
                get;
                private set;
            }
 
            internal void Add(DateTime time)
            {
                var @event = Events.FirstOrDefault(x => x.Time == time);
                if(@event == null)
                {
                    @event = new EventModel()
                    {
                        Count = 0,
                        Time = time
                    };
                    Events.Add(@event);
                }
                @event.Count++;
            }
 
            public override string ToString()
            {
                return Title;
            }
        }
 
        public class EventModel
        {
            public DateTime Time
            {
                get;
                set;
            }
 
            public int Count
            {
                get;
                set;
            }
 
            public override string ToString()
            {
                return Time.ToShortTimeString() + " Total: " + Count.ToString();
            }
        }
 
        public CrosstabClassificationModel()
        {
            Approaches = new List<ApproachModel>();
        }
 
        public DateTime Date
        {
            get;
            set;
        }
 
        public List<ApproachModel> Approaches
        {
            get;
            private set;
        }
 
        public ApproachModel this[string Name]
        {
            get
            {
                return Approaches.FirstOrDefault(x => x.Name == Name);
            }
        }
 
        public override string ToString()
        {
            return Date.ToShortDateString();
        }
    }

No answers yet. Maybe you can help?

Tags
General Discussions
Asked by
Andrew
Top achievements
Rank 2
Share this question
or