问题
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