datagridview显示combox

private void Form1_Load(object sender, EventArgs e) { string connstring = "server=.;database=Accp;uid=sa;pwd=123"; SqlConnection con = new SqlConnection(connstring); SqlDataAdapter da = new SqlDataAdapter("select ID,Name from userInfo u inner join Type t on u.MemID=t.TypeID", con); DataTable dt = new DataTable(); da.Fill(dt); this.dataGridView1.DataSource = dt; for (int i = 0; i < this.dataGridView1.Rows.Count; i++) { DataGridViewComboBoxColumn cc = this.dataGridView1.Rows[i].Cells["MemType"].OwningColumn as DataGridViewComboBoxColumn; cc.DataSource = GetTable(); cc.DisplayMember = "TypeName"; cc.ValueMember = "TypeID"; } } public DataTable GetTable() { string connstring = "server=.;database=Accp;uid=sa;pwd=123"; SqlConnection con = new SqlConnection(connstring); SqlDataAdapter da = new SqlDataAdapter("select TypeID,TypeName from type", con); DataTable dt = new DataTable(); da.Fill(dt); return dt; }

 

好像效率可能有点问题,不知道大家有更好的办法不

效果图

datagridview显示combox_第1张图片

你可能感兴趣的:(JOIN,server,String,object)