加载datagridview并添加组合框

时间:2012-09-27 18:05:39

标签: c# datagridview ado.net

这是我用来创建datagridview然后添加2个组合框的方法。从其他类加载三个DataTable。

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;

        }

1 个答案:

答案 0 :(得分: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; }
}