用datatable处理DataGridView比较方便,但是今天在帮朋友做时,没用数据库,用的List集合去做了,结果发现List集合不能转为datatable
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataTable dt2 = new DataTable();
dt2.Columns.Add("Name");
dt2.Columns.Add("Age");
for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
{
DataRow dr = dt2.NewRow();
dr["Name"] = dataGridView1.SelectedRows[i].Cells[0].Value;
dr["Age"] = dataGridView1.SelectedRows[i].Cells[1].Value;
dt2.Rows.Add(dr);
}
Form2 fr = new Form2(dt2);
fr.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
List<person> list1 = new List<person>
{
new person{Name="123",Age=12},
new person{Name="234",Age=12}
};
dataGridView1.DataSource = list1;
}
}
}