private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataColumn c1 = new DataColumn("序号", typeof(string));
DataColumn c2 = new DataColumn("名称", typeof(string));
DataColumn c3 = new DataColumn("职位", typeof(string));
dt.Columns.Add(c1);
dt.Columns.Add(c2);
dt.Columns.Add(c3);
dt.Rows.Add(1,"测试1","工人");
dt.Rows.Add(2, "测试2", "农民");
dt.Rows.Add(3, "测试3", "学生");
dt.Rows.Add(4, "测试4", "工人");
//禁止添加行,防止显示空白行
dataGridView1.AllowUserToAddRows = false;
//选择是否自动创建列
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = dt;
}
标签:控件,Rows,DataColumn,DataGridview,Add,new,dt,Columns,winform From: https://www.cnblogs.com/wugh8726254/p/17561160.html