这是我用来创建数据网格视图然后添加 2 个组合框的方法。三个 DataTables 从其他类中加载。
public void GetWorkorderItems()
{
Workorders wo = new Workorders();
ItemTable = wo.LoadWorkorderItemsTable(_ID);
this.datagridWorkorderItems.DataSource = ItemTable;
this.datagridWorkorderItems.AutoGenerateColumns = true;
this.datagridWorkorderItems.Columns[0].Visible = false;
this.datagridWorkorderItems.Columns[1].HeaderText = "Qty";
this.datagridWorkorderItems.Columns[1].Width = 100;
this.datagridWorkorderItems.Columns[2].HeaderText = "Part";
this.datagridWorkorderItems.Columns[2].Width = 100;
this.datagridWorkorderItems.Columns[3].HeaderText = "Labor";
this.datagridWorkorderItems.Columns[3].Width = 100;
this.datagridWorkorderItems.Columns[4].HeaderText = "Price";
this.datagridWorkorderItems.Columns[4].Width = 150;
this.datagridWorkorderItems.Columns[5].HeaderText = "Description";
this.datagridWorkorderItems.Columns[5].Width = 150;
Parts part = new Parts();
DataTable partdata = new DataTable();
partdata = part.LoadPartTable();
DataGridViewComboBoxColumn pcb = (DataGridViewComboBoxColumn)this.datagridWorkorderItems.Columns[2];
pcb.DataSource = partdata;
pcb.DisplayMember = "PartName";
pcb.ValueMember = "PartID";
datagridWorkorderItems.Columns.Add(pcb);
Labor labor = new Labor();
DataTable data = new DataTable();
data = labor.LoadLaborTable();
DataGridViewComboBoxColumn cb = (DataGridViewComboBoxColumn)this.datagridWorkorderItems.Columns[3];
cb.DataSource = data;
cb.DisplayMember = "LaborItem";
cb.ValueMember = "LaborID";
datagridWorkorderItems.Columns.Add(cb);
}
当代码创建 DataGridViewComboBox 对象时,代码退出该方法并且不添加或加载组合框。也没有抛出异常。
有什么帮助吗?
编辑* * ** DataGridViewTextBoxColumn col0 = (DataGridViewTextBoxColumn)this.datagridWorkorderItems.Columns[0]; this.datagridWorkorderItems.Columns[0].DataPropertyName = "WOItemID"; this.datagridWorkorderItems.Columns[0].Visible = false; this.datagridWorkorderItems.Columns.Add(col0);
DataGridViewTextBoxColumn col1 = (DataGridViewTextBoxColumn)this.datagridWorkorderItems.Columns[1];
this.datagridWorkorderItems.Columns[1].DataPropertyName = "Quantity";
this.datagridWorkorderItems.Columns[1].HeaderText = "Qty";
this.datagridWorkorderItems.Columns[1].Width = 100;
this.datagridWorkorderItems.Columns.Add(col1);
DataGridViewComboBoxColumn pcb = (DataGridViewComboBoxColumn)this.datagridWorkorderItems.Columns[2];
pcb.DataSource = partdata;
pcb.DisplayMember = "PartName";
pcb.ValueMember = "PartID";
datagridWorkorderItems.Columns.Add(pcb);
如果我理解的话,这可能是构建列的方法。
***修复了有效的代码* ** * **
public void GetWorkorderItems()
{
Workorders wo = new Workorders();
ItemTable = wo.LoadWorkorderItemsTable(_ID);
Parts part = new Parts();
DataTable partdata = new DataTable();
partdata = part.LoadPartTable();
Labor labor = new Labor();
DataTable data = new DataTable();
data = labor.LoadLaborTable();
this.datagridWorkorderItems.AutoGenerateColumns = false;
DataGridViewTextBoxColumn col0 = new DataGridViewTextBoxColumn();
col0.DataPropertyName = "WOItemID";
col0.Visible = false;
this.datagridWorkorderItems.Columns.Add(col0);
DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
col1.DataPropertyName = "Quantity";
col1.HeaderText = "Qty";
col1.Width = 100;
this.datagridWorkorderItems.Columns.Add(col1);
DataGridViewComboBoxColumn col2 = new DataGridViewComboBoxColumn();
col2.DataPropertyName = "PartID";
col2.DataSource = partdata;
col2.DisplayMember = "PartName";
col2.ValueMember = "PartID";
col2.HeaderText = "Part";
datagridWorkorderItems.Columns.Add(col2);
DataGridViewComboBoxColumn col3 = new DataGridViewComboBoxColumn();
col3.DataPropertyName = "LaborID";
col3.DataSource = data;
col3.DisplayMember = "LaborItem";
col3.ValueMember = "LaborID";
col3.HeaderText = "Labor";
datagridWorkorderItems.Columns.Add(col3);
DataGridViewTextBoxColumn col4 = new DataGridViewTextBoxColumn();
col4.DataPropertyName = "Price";
col4.HeaderText = "Price";
col4.Width = 100;
this.datagridWorkorderItems.Columns.Add(col4);
DataGridViewTextBoxColumn col5 = new DataGridViewTextBoxColumn();
col5.DataPropertyName = "Description";
col5.HeaderText = "Description";
col5.Width = 100;
this.datagridWorkorderItems.Columns.Add(col5);
this.datagridWorkorderItems.DataSource = ItemTable;
}