c#如何将datagridview里的每行数据存到list列表里面

请教如何将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);

}

4个回答

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);

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐