Can't figure out how to get a column to appear as a Combobox with static options

馋奶兔 提交于 2020-01-07 15:46:55

问题


 private List<string> mylist = new List<string>(new string[] { "Visitor Seen", "Update Reason", "Ghost Sighted! HELP!" });

    private void setupDataGridView()
    {
        dataGridView1.Columns.Add("ID", "Visitor ID");
        dataGridView1.Columns.Add("VisitorName", "Visitor Name");
        dataGridView1.Columns.Add("SignInTime", "Sign In Time");
        dataGridView1.Columns.Add("Reason", "Reason For Visit");
        DataGridViewComboBoxColumn comboCol = new DataGridViewComboBoxColumn();
        comboCol.Name = "comboActionableItem";
        comboCol.HeaderText = "Action";
        comboCol.DataSource = mylist;
        dataGridView1.Columns.Add(comboCol);
    }

If I use autogenerated columns, everything works. However I was told that to add a custom column that is not coming from a datasource, we need to setup the DataGridView and manually set each column and then iterate each row from my DataTable and insert it into the DGV.

Below is my code for autogenerating the view (and it works perfectly)

    private void loadData()
    {
        OleDbConnection conn = new OleDbConnection(@"Provider = Microsoft.ACE.OLEDB.12.0;User Id=;Password=;Data Source=" + fileName);
        conn.Open();
        OleDbDataAdapter dataAdapter = new OleDbDataAdapter(queryText, conn);
        DataSet ds = new DataSet();
        dataAdapter.Fill(ds);
        dataGridView1.DataSource = ds.Tables[0];
        conn.Close();
    }

I want to add a combobox with 2 static values (Visitor Seen) and (Update Reason).

However when I run the app, I don't see any values in my dropdown.


回答1:


Try using Items.AddRange

comboCol.Items.AddRange("Visitor Seen", "Update Reason", "Ghost Sighted! HELP!");


来源:https://stackoverflow.com/questions/38921880/cant-figure-out-how-to-get-a-column-to-appear-as-a-combobox-with-static-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!