0

これは、datagridview を作成してから 2 つのコンボボックスを追加するために使用している方法です。3 つの 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 オブジェクトの作成に達すると、コードはメソッドを終了し、コンボボックスを追加またはロードしません。スローされる例外もありません。

何か助けはありますか?

EDIT* * ** 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;

        }
4

1 に答える 1

1

次のようなことを試してください:

public ExampleForm()
    {
        InitializeComponent();
        datagridWorkorderItems.AutoGenerateColumns = false;

        DataGridViewTextBoxColumn qtyColumn = new DataGridViewTextBoxColumn();
        qtyColumn.DataPropertyName = "Qty";
        qtyColumn.HeaderText = "Qty";
        datagridWorkorderItems.Columns.Add(qtyColumn);

        DataGridViewComboBoxColumn partColumn = new DataGridViewComboBoxColumn();
        partColumn.Items.Add(new Part() { ID = 0, PartName = "Tire" });
        partColumn.Items.Add(new Part() { ID = 1, PartName = "Motor" });
        partColumn.HeaderText = "Part";
        partColumn.DataPropertyName = "PartID";
        partColumn.ValueMember = "ID";
        partColumn.DisplayMember = "PartName";
        datagridWorkorderItems.Columns.Add(partColumn);

        List<WorkOrder> workOrders = new List<WorkOrder>();
        workOrders.Add(new WorkOrder() { Qty = 0, PartID = 0});
        workOrders.Add(new WorkOrder() { Qty = 2, PartID = 1});

        datagridWorkorderItems.DataSource = workOrders;
    }
}

public class WorkOrder
{
    public int Qty { get; set; }
    public int PartID { get; set; }
}

public class Part
{
    public int ID { get; set; }
    public string PartName { get; set; }
}
于 2012-09-27T18:31:04.347 に答える