Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I have two lists, i want to bind it into a single gridview in c#.

public class timeSlots
{
   public string StartTimeSlots { get; set; }
        public List<DateTime> StartTimeSlotList
        {
            get { return StartTimeSlots.Split(',').Select(DateTime.Parse).ToList();  }

            set
            {
                StartTimeSlots = StartTimeSlots.Remove(StartTimeSlots.Length - 1);
            }
        }

        public string EndTimeSlots { get; set; }
        public List<DateTime> EndTimeSlotList
        {
            get { return EndTimeSlots.Split(',').Select(DateTime.Parse).ToList();  }

            set
            {
                EndTimeSlots = EndTimeSlots.Remove(EndTimeSlots.Length - 1);
            }
        }
}


What I have tried:

i want to bind StartTimeSlotList and EndTimeSlotList into a single gridview.

Can any one please help me.


Thanks
Posted
Updated 29-Aug-16 23:05pm
Comments

Wrong approach!

timeSlot class should be defined as:
C#
public class TimeSlot
{
	private DateTime startDT = new DateTime(1901,1,1); //defualt start time
	private DateTime endDT = new DateTime(1901,1,1); //defualt end time
	
	public TimeSlot()
	{
		//default constructor
	}
	
	public TimeSlot(DateTime sDT, DateTime eDT)
	{
		startDT = sDT;
		endDT = eDT;
	}
	
	public DateTime StartTime
	{
		get { return startDT; }
		set { startDT = value; }
	}

	public DateTime EndTime
	{
		get { return endDT; }
		set { endDT = value; }
	}
}

Next, you have to create List<TimeSlot> class:
C#
List<TimeSlot> timeslots = new List<TimeSlot>();
DateTime dt = DateTime.Now;
//insert sample data 
for (int i=-200;i<0; i+=5)
{
    timeslots.Add(new TimeSlot(dt.AddHours(i), dt.AddHours(i+(i%2==0?1:2))));
}


Finally:
C#
DataGridView1.DataSource = timeslots;
 
Share this answer
 
use something like below

C#
var timeSlots = StartTimeSlots.Join(EndTimeSlots,st => StartTimeSlots.IndexOf(st), et => EndTimeSlots.IndexOf(et), (st, et) => new { newst = st, newet = et }).ToList();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900