combox也有DataSource属性,直接把dataGridView1第一列的集合赋给combox就可以了,然后给combox的comboBoxGroup.ValueMember = "列名";属性赋值,两行代码就能搞定,不需要循环!
我是这样实现的,希望对你有帮助
private void datagridviewtest_Load(object sender, EventArgs e)
{
sql = " select * from account_user_t ";
ds = MySqlHelper.ExecuteDataset(constr, sql);
dataGridView1.DataSource = ds.Tables[0];
if (dataGridView1.RowCount > 0)
{
comboBox1.Items.Clear();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
comboBox1.Items.Add(ds.Tables[0].Rows[i]["UserName"].ToString());
}
}
}