ฉันกำลังพยายามสร้าง DataGridView ที่ผูกไว้กับ DataTable โดยที่หนึ่งคอลัมน์คือ ComboBox รหัสทำงาน แต่ฉันได้รับข้อผิดพลาดต่อไปนี้หลังจากการผูก (ไม่ใช่เมื่อข้อมูลถูกผูกไว้): System.ArgumentException: ค่า DataGridViewComboBoxCell ไม่ถูกต้อง
ใน DataGridView หนึ่งในคอลัมน์คือ DataGridViewComboBoxColumn ที่ใช้ enum (ชื่อ โครงสร้างประเภท) เป็นแหล่งที่มา:
// ColumnStructure
//
this.ColumnStructure.ValueType = typeof(structureType);
this.ColumnStructure.DataSource = Enum.GetValues(typeof(structureType));
this.ColumnStructure.HeaderText = "Structure";
this.ColumnStructure.Name = "ColumnStructure";
this.ColumnStructure.DataPropertyName = "Structure";
//
เมื่อฉันเติม DataGridView โดยไม่ใช้ DataTable มันก็ทำงานได้ดี:
structureType? structure = GetStructure(part);
dgvObjectTypes.Rows.Add(name, type, structure, count);
ตอนนี้ฉันต้องการใช้ DataTable แทน แต่ไม่สามารถใช้งานได้ DataTable ถูกสร้างขึ้นดังนี้:
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Type", typeof(string));
table.Columns.Add("Structure", typeof(DataGridViewComboBoxCell));
table.Columns.Add("Count", typeof(int));
คอลัมน์อื่นๆ ใช้งานได้ดี แต่ฉันไม่สามารถทำให้คอลัมน์ "โครงสร้าง" ทำงานได้ ต่อไปนี้เป็นวิธีที่ฉันพยายามสร้าง Combobox:
var cb = new DataGridViewComboBoxCell();
cb.ValueType = typeof(structureType);
cb.DataSource = Enum.GetValues(typeof(structureType));
cb.Value = (structureType)structure;
หลังจากนั้น ฉันแค่สร้างแถวสำหรับตารางและตั้งค่าตารางเป็นแหล่งข้อมูลสำหรับ DataGridView:
table.Rows.Add(name, type, cb, count);
dgv.DataSource = table;
ฉันได้อ่านโพสต์จำนวนมากที่มีการระบุว่าการใช้ enum ในคอมโบบ็อกซ์ทำให้เกิดปัญหา (ตัวอย่าง: DataGridView เชื่อมโยงกับ DataTable ด้วยคอลัมน์ Combobox ตาม enum) แต่ดูเหมือนจะไม่เป็นเช่นนั้น ฉันยังพยายามใช้อาร์เรย์ของสตริงที่พิมพ์อย่างชัดเจน แต่ยังคงได้รับข้อผิดพลาดเดียวกัน ฉันคิดว่าฉันกำลังทำอะไรผิดกับ DataGridViewComboBoxCell
ปัญหาอาจเกิดจากอะไร?