写入固定值DataGridViewComboBoxCell cell1 = dataGridView1[1, i] as DataGridViewComboBoxCell; DataGridViewComboBoxCell cell2 = dataGridView1[2, i] as DataGridViewComboBoxCell; for (int j = 1; j <= 8; j++) { if (j == 1) { DataTable dtttt = new DataTable(); dtttt.Columns.Add("v");//键值对value对应key text对应画面显示 dtttt.Columns.Add("text"); for (int m = 1; m < 5; m++) { DataRow r = dtttt.NewRow(); r["v"] = "0" + m; r["text"] = "0" + m; dtttt.Rows.Add(r); } cell1.DataSource = dtttt; cell1.DisplayMember = "text"; cell1.ValueMember = "v";
} if (j == 2) {
cell2.DisplayMember = "text"; cell2.ValueMember = "v"; cell2.Items.Add(new { v = "1", text = "1 : 工作日" }); cell2.Items.Add(new { v = "2", text = "2 : 星期六" }); cell2.Items.Add(new { v = "3", text = "3 : 星期日" }); cell2.Items.Add(new { v = "4", text = "4 : 其它" }); }
下面的是从数据库绑定未验证
SqlDataAdapter da = new SqlDataAdapter("select * from Customers", "Data Source=.;Initial Catalog=Northwind;Integrated Security=True"); DataSet ds = new DataSet(); da.Fill(ds); DataTable dt = ds.Tables[0]; comboBox1.DataSource = dt; comboBox1.DisplayMember = "city"; //要显示的字段名 //如果是Web程序的话,加上comboBox1.DataBind();