将DataGridView列数据插入Combobox

时间:2014-06-18 07:18:16

标签: c# datagridview combobox ms-access-2007

我正在编写这个程序,我需要将特定列的数据存储到组合框中。数据库表中只有一列,需要在组合框中填充。

我正在尝试的守则是;

private void populateList()
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            cmbUserNames.Items.Add(row.Cells[2].Value.ToString());
        }  
    }

我创建了一个DataGridView,它在表单启动时从数据中填充自己。我已经创建了上面的方法,当我在FormLoad事件中调用它时,在加载DataGridView之后,它会抛出异常。

我期待着解决方案。谢谢!

1 个答案:

答案 0 :(得分:0)

您可以尝试使用以下内容代替:

private void populateList()
{       
    cmbUserNames.DataSource = yourDataSource;
    //In the event you need specify column to be displayed/or value add the following
    cmbUserNames.ValueMember = "ColumnToBevalue";
    cmdUserNames.DisplayMember = "ColumnToDisplay";
}
相关问题