Reputation: 87
I want to add two different Values in Same column in DataTable.
row0["sessionID"] = ss.session_id;
row0["sessionID"] = se.session_id;
Scenario is I have to sessions 2015-16 and 2016-17 they both have a start date and a end date and ID. I want if start date of 2nd session occurs than ID of 2nd session must be added into sessionID
column.
For Example: the ID of session 2015-16 is 50 and the ID of session 2016-17 is 70 then the DataTable must be look like this:
SessionID Session Date
____________________________
50 2015-10-01
50 2015-11-01
50 2015-12-01
50 2016-01-01
50 2016-02-28
70 2016-03-01
70 2016-04-01
70 2016-05-01
. .
. .
70 2017-03-31
Date is Adding fine but I am unable to add Session ID. How can I add it?
Here is my complete code:
var sessionsFrom = (from o in _session.GetAll()
where o.session_name == ddl_SessionFrom.SelectedItem.Text
&& o.branch_id == Convert.ToInt32(ddl_search_branch.SelectedValue)
select o).ToArray();
var sessionsTo = (from o in _session.GetAll()
where o.session_id == Convert.ToInt32(ddl_SessionTo.SelectedValue)
&& o.branch_id == Convert.ToInt32(ddl_search_branch.SelectedValue)
select o).ToArray();
DataTable dt = new DataTable();
dt.Columns.Add("id").DataType = typeof(Int32);
dt.Columns.Add("SessionFrom");
dt.Columns.Add("SessionTo");
dt.Columns.Add("Month");
dt.Columns.Add("totalStudent").DataType = typeof(Int32);
dt.Columns.Add("totalAdmission").DataType = typeof(Int32);
dt.Columns.Add("totLefts").DataType = typeof(Int32);
dt.Columns.Add("sessionID").DataType = typeof(Int32);
foreach (var ss in sessionsFrom)
foreach(var se in sessionsTo)
{
var row0 = dt.NewRow();
row0["totalStudent"] = totalStudent;
row0["SessionFrom"] = ss.session_name;
row0["SessionTo"] = se.session_name;
row0["sessionID"] = ss.session_id;
row0["sessionID"] = se.session_id;
id = id + 1;
row0["id"] = id;
ts = totalStudent;
dt.Rows.Add(row0);
for (DateTime i = ss.session_startdate; i < se.session_enddate; i = i.AddMonths(1))
{
int a = i.Month;
int b = i.Year;
var row = dt.NewRow();
row["SessionFrom"] = ss.session_name;
row["SessionTo"] = se.session_name;
row["sessionID"] = se.session_id;
.
.
.
Upvotes: -1
Views: 928
Reputation: 3979
The following code creates a DataTable which reach your example table in your question:
//Help Method to iterate over Dates
public IEnumerable<DateTime> EachMonth(DateTime from, DateTime thru)
{
for(var month = from.Date; month.Date <= thru.Date; month = month.AddMonth(1))
yield return month;
}
DataTable dt = new DataTable();
dt.Columns.Add("SessionID").DataType = typeof(Int32);
dt.Columns.Add("SessionDate").DataType = typeof(DateTime);
// Iterate over all dates from 01.01.2015 to 01.03.2017
foreach (DateTime date in EachMonth(new DateTime(2015,1,1), new DateTime(2017,3,1))
{
DataRow row = dt.NewRow();
// Check if the magic Date is reached. After this ID jumps to 70
if (date.CompareTo(new DateTime(2016, 2, 28)) <= 0)
{
row["SessionID"] = 50;
}
else
{
row["SessionID"] = 70;
}
row["SessionDate"] = date;
dt.Rows.Add(row);
}
This is the hardcoded way to reach your table preferred in your question.
I hope this helps you. You simple add one row each month. If the month is higher then value X your Id is set to another value.
Upvotes: 0