无法以编程方式将组合框添加到datagridview列 - C# - 抛出异常

时间:2012-01-04 19:06:12

标签: c# combobox datagridcomboboxcolumn

似乎我遗漏了以下代码中的内容:

 DataTable dt = new DataTable();
 DataColumn dt_col_project_id = new DataColumn("Project ID", typeof(Int32));
 DataColumn dt_col_activate = new DataColumn("On/Off", typeof(bool));
 DataColumn dt_col_nature = new DataColumn("Nature", typeof(String));
 DataColumn dt_col_name = new DataColumn("Circle List", typeof(String));
 DataColumn dt_col_keywords = new DataColumn("Keywords", typeof(String));
 DataGridViewComboBoxColumn dt_col_automation = new DataGridViewComboBoxColumn();

                //setup combobox                        
                dt_col_automation.HeaderText = "Automation";
                dt_col_automation.Name = "dgv_jobs_col_automation";
                    dt_col_automation.Items.AddRange(
                        "Once every 5 minutes",
                        "Once every 10 minutes",
                        "Once every 15 minutes",
                        "Once every 30 minutes",
                        "Once every hour",
                        "Once every 2 hours",
                        "Once every 3 hours",
                        "Once every 4 hours",
                        "Once every 5 hours",
                        "Once every 6 hours",
                        "Once every 7 hours",
                        "Once every 8 hours",
                        "Once every 9 hours",
                        "Once every 10 hours",
                        "Once every 11 hours",
                        "Once every 12 hours",
                        "1",
                        "2",
                        "3",
                        "4",
                        "5",
                        "6",
                        "7",
                        "8",
                        "9",
                        "10",
                        "11",
                        "12",
                        "13",
                        "14",
                        "15",
                        "16",
                        "17",
                        "18",
                        "19",
                        "20",
                        "21",
                        "22",
                        "23",
                        "24"
                    );



                    dt.Columns.Add(dt_col_project_id); // Create a string column dynamically
                    dt.Columns.Add(dt_col_activate); // Create a string column dynamically
                    dt.Columns.Add(dt_col_nature); // create an int column dyn
                    dt.Columns.Add(dt_col_name); // create a double column dyn
                    dt.Columns.Add(dt_col_keywords); // create a double column dyn
                    dt.Columns.Add(dt_col_automation); // create a double column dyn

因为它抛出了这些例外。我做错了什么?

enter image description here

1 个答案:

答案 0 :(得分:2)

您正在尝试将组合框列添加到绑定到GridView的DataTable,而不是Gridview本身。 DataGridViewComboBoxColumn不从DataColumn继承,您需要在GridView中设置列,而不是在DataTable上设置。