Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

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

 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try using Items.AddRange

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...