请教如何将datagridview里的每行数据(从一个excel中读出来的)存到list列表里面(list里面存放的是student的对象),部分代码如下:
string sql_select = " SELECT StudentID,StudentName,StudentBirthday,StudentAddress FROM [Sheet1$]";
GetDataFromExcel GD = new GetDataFromExcel();
DataTable dt = GD.getData("students.xlsx", sql_select).Tables[0];
dataGridView.DataSource = dt;
dataGridView.AllowUserToResizeColumns = true;
dataGridView.Font = new Font("Arial", 11);
Student stu = new Student();
List listStudent = new List();
foreach (DataGridViewRow rows in dataGridView.Rows )
{
Student stu = new Student(rows.Cells[0].Value.ToString(),
rows.Cells[1].Value.ToString(),
rows.Cells[2].Value.ToString(),
rows.Cells[3].Value.ToString());
listStudent.Add(stu);
}
Winform 还是 WPF?
用泛型
List listStudent=new List();
你既然已经拿到datatable了,为啥还要从gridview里面取,直接从datatable不是更好
List<student> listStudent=new List<student>();
List listStudent=new List();
student.XX = rows.Cells[1].Value.ToString(),
listStudent.add(student);