无法以编程方式将组合框添加到 datagridview 列 - C# - 引发异常
我似乎在下面的代码中遗漏了一些内容:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试将组合框列添加到绑定到 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.