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

发布于 2024-12-25 01:01:11 字数 2860 浏览 7 评论 0原文

我似乎在下面的代码中遗漏了一些内容:

 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

因为它抛出了这些异常。我做错了什么?

在此处输入图像描述

It seems I am missing something from my code below:

 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

Because it's throwing these exceptions. What am I doing wrong?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一梦等七年七年为一梦 2025-01-01 01:01:11

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

You are trying to add a combobox column to the DataTable that is bound to the GridView, not the Gridview itself. DataGridViewComboBoxColumn does not inherit from DataColumn, you need to set up the columns in the GridView, not on the DataTable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文