winform中DataGridview控件

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;


}

 

 

 

posted @ 2023-07-17 20:37  .net&new  阅读(43)  评论(0编辑  收藏  举报