ailenlan
ailenlan
采纳率0%
2017-06-07 01:54 阅读 4.0k

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条回答 默认 最新

  • m294955408 偷偷的挂掉 2017-06-07 01:54

    Winform 还是 WPF?

    点赞 评论 复制链接分享
  • wodewojueding wodewojueding 2017-06-07 02:06

    用泛型
    List listStudent=new List();

    你既然已经拿到datatable了,为啥还要从gridview里面取,直接从datatable不是更好

    点赞 评论 复制链接分享
  • wodewojueding wodewojueding 2017-06-07 02:07
    
    List<student> listStudent=new List<student>();
    
    点赞 评论 复制链接分享
  • qq_35498454 xhl_2017 2017-06-07 02:25

    List listStudent=new List();

    student.XX = rows.Cells[1].Value.ToString(),

    listStudent.add(student);

    点赞 评论 复制链接分享

相关推荐