DataTable dt = new DataTable();
dt.Columns.Add("a", typeof(string));
dt.Columns.Add("b", typeof(string));
for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr["a"] = "1";
dr["b"] = i.ToString();dt.Rows.Add(dr);
}DataTable dt2 = new DataTable();
dt2.Columns.Add("id", typeof(string));
dt2.Columns.Add("name", typeof(string));
for (int i = 0; i < 5; i++)
{
DataRow dr2 = dt2.NewRow();
dr2["id"] = i.ToString();
dr2["name"] = "胡" + i.ToString();dt2.Rows.Add(dr2);
}
foreach (DataRow item in dt2.Rows)
{
string str = item["name"].ToString();
}// 列表对应的列名称
DataGridViewComboBoxColumn cbocl = dataGridView1.Columns["Column2"] as DataGridViewComboBoxColumn;
cbocl.DataSource = dt2;
cbocl.DisplayMember = "name";
cbocl.ValueMember = "id";// dataGridView1.Rows.Add(new object[] { "1","2"});
dataGridView1.DataSource = dt;
winfrom列表添加下拉框
最新推荐文章于 2024-05-10 15:31:30 发布