[英]Is it possible to store a multi-dimensional array to an object?
Here are my classes:这是我的课程:
public class Type_WorkSched
{
public string EmployeeID { get; set; }
public string RecordID { get; set; }
public Type_Schedule[] Schedule { get; set; }
}
public class Type_Schedule
{
public string Days { get; set; }
public string TimeIn_AM { get; set; }
public string TimeOut_AM { get; set; }
public string TimeIn_PM { get; set; }
public string TimeOut_PM { get; set; }
}
I want to store an array of MySchedule
into an object MyWorkSched
.我想将MySchedule
数组存储到对象MyWorkSched
。
But I get only the last value of the array.但我只得到数组的最后一个值。
GV_WorkSched
is the name of the datagrid. GV_WorkSched
是数据网格的名称。
Type_Schedule[] MySchedule = new Type_Schedule[GV_WorkSched.RowCount-1];
Type_WorkSched MyWorkSched = new Type_WorkSched();
MyWorkSched.EmployeeID = Txt_EmployeeID.Text;
MyWorkSched.RecordID = Txt_RecordNumber.Text;
for (int i = 0; i < GV_WorkSched.RowCount-1; i++)
{
MySchedule[i] = new Type_Schedule();
MySchedule[i].Days = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "Days"));
MySchedule[i].TimeIn_AM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeIn_AM"));
MySchedule[i].TimeOut_AM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeOut_AM"));
MySchedule[i].TimeIn_PM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeIn_PM"));
MySchedule[i].TimeOut_PM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeOut_PM"));
MyWorkSched.Schedule = new Type_Schedule[] { MySchedule[i] };
}
Or is there another way to do it?或者有其他方法可以做到吗?
You should do it as below你应该这样做
Type_Schedule[] MySchedule = new Type_Schedule[GV_WorkSched.RowCount];
Type_WorkSched MyWorkSched = new Type_WorkSched();
MyWorkSched.EmployeeID = Txt_EmployeeID.Text;
MyWorkSched.RecordID = Txt_RecordNumber.Text;
for (int i = 0; i < GV_WorkSched.RowCount; i++)
{
MySchedule[i] = new Type_Schedule();
MySchedule[i].Days = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "Days"));
MySchedule[i].TimeIn_AM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeIn_AM"));
MySchedule[i].TimeOut_AM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeOut_AM"));
MySchedule[i].TimeIn_PM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeIn_PM"));
MySchedule[i].TimeOut_PM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeOut_PM"));
}
MyWorkSched.Schedule = MySchedule;
Your problem is on this line你的问题在这条线上
MyWorkSched.Schedule = new Type_Schedule[] { MySchedule[i] }
At the end of each iteration, you create a new array, with only one value in it (the latest).在每次迭代结束时,您创建一个新数组,其中只有一个值(最新的)。 When you get out of it this is the only one that remains.当你摆脱它时,这是唯一剩下的。
Try the following.请尝试以下操作。 I totally removed the intermediate array as I think it was part of the confusion.我完全删除了中间数组,因为我认为这是混乱的一部分。
Type_WorkSched MyWorkSched = new Type_WorkSched();
MyWorkSched.Schedule = new Type_Schedule[GV_WorkSched.RowCount];
MyWorkSched.EmployeeID = Txt_EmployeeID.Text;
MyWorkSched.RecordID = Txt_RecordNumber.Text;
for (int i = 0; i < GV_WorkSched.RowCount-1; i++)
{
MyWorkSched.Schedule[i] = new Type_Schedule();
MyWorkSched.Schedule[i].Days = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "Days"));
MyWorkSched.Schedule[i].TimeIn_AM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeIn_AM"));
MyWorkSched.Schedule[i].TimeOut_AM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeOut_AM"));
MyWorkSched.Schedule[i].TimeIn_PM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeIn_PM"));
MyWorkSched.Schedule[i].TimeOut_PM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(i, "TimeOut_PM"));
}
Type_Schedule[] MySchedule = new Type_Schedule[GV_WorkSched.RowCount-1];
Type_WorkSched MyWorkSched = new Type_WorkSched();
MyWorkSched.EmployeeID = Txt_EmployeeID.Text;
MyWorkSched.RecordID = Txt_RecordNumber.Text;
MyWorkSched.Schedule = new Type_Schedule[] {
new Type_Schedule {
Days = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(GV_WorkSched.RowCount-1, "Days")),
TimeIn_AM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(GV_WorkSched.RowCount-1, "TimeIn_AM")),
TimeOut_AM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(GV_WorkSched.RowCount-1, "TimeOut_AM")),
TimeIn_PM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(GV_WorkSched.RowCount-1, "TimeIn_PM")),
TimeOut_PM = Convert.ToString(GV_WorkSched.GetRowCellDisplayText(GV_WorkSched.RowCount-1, "TimeOut_PM"))
};
};
Since you are any ways trying to fetch the last value only why do you want to reassign a new Type_Schedule array every time in the loop.Avoid the loop and if you want to store the entire schedule data then use a loop.
MyWorkSched.Schedule = new Type_Schedule[] { MySchedule[i] };// this line if inside the loop at the end of the loop will give the same result as above
I suggest to use List<>
instead.我建议改用List<>
。 First, redeclare your type as首先,将您的类型重新声明为
public class Type_WorkSched
{
public string EmployeeID { get; set; }
public string RecordID { get; set; }
public List<Type_Schedule> Schedule { get; set; }
// In case you really need it as Array
public Type_Schedule[] ScheduleAsArray()
{
return Schedule.ToArray();
}
}
Second, iterate your grid in foreach
way其次,以foreach
方式迭代你的网格
foreach (DataGridViewRow row in GV_WorkSched.Rows)
{
var schedule = new Type_Schedule
{
Days = row.Cells["Days"].Value.toString(),
//...
};
MyWorkSched.Schedule.Add(schedule);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.