C#手写DataTable用于DataGridView或FastReport的数据源
private void btnGenDT_Click(object sender, EventArgs e) { //创建表对象 DataTable dt = new DataTable(); //设置表列 dt.Columns.Add("姓名"); dt.Columns.Add("性别"); dt.Columns.Add("年龄"); //先插一行试试 DataRow dr1 = dt.NewRow(); dr1["姓名"] = "张三"; dr1["性别"] = "男"; dr1["年龄"] = "18"; dt.Rows.Add(dr1); for (int i = 0; i < 5; i++) { DataRow dr = dt.NewRow(); dr["姓名"] = "李四~"+i.ToString(); dr["性别"] = i%2==0?"女":"男"; dr["年龄"] = i+20; dt.Rows.Add(dr); } dataGridView1.DataSource = dt; #region 测试 ////输出测试 //foreach (DataRow row in dataTable1.Rows) //{ // for (int i = 0; i < dataTable1.Columns.Count; i++) // { // Console.Write(row[i] + " "); // } // Console.WriteLine(); //} //Console.ReadKey(); #endregion }