0

我需要将 DataGridView 绑定到将通过 Oledb 填充的数据表。

这部分不是问题,但我想做的是替换 DataGridView 的一些标准列,并将它们替换为 DataGridViewComboBox 列。

然后这些列将拥有自己的数据源,这将允许最终用户将默认数据值更改为项目集合中的数据值之一。

有没有人为这种任务提供任何像样的链接或教程?

任何帮助将不胜感激。

4

1 回答 1

0

我设法让测试工作。我希望这可以帮助其他需要帮助的人。这是一个非常基本的测试。但它有效。

public DataTable Orders = new DataTable("Order_Table");

        private void Form1_Load(object sender, EventArgs e)
        {


            // Create Orders Table
            Orders.Columns.Add("Order_ID");
            Orders.Columns.Add("Product_ID");


            DataRow row = Orders.NewRow();

            row["Order_ID"] = "1";
            row["Product_ID"] = "23";

            Orders.Rows.Add(row);

            DataRow row1 = Orders.NewRow();

            row1["Order_ID"] = "2";
            row1["Product_ID"] = "24";

            Orders.Rows.Add(row1);

            // Bind to Orders Table
            BindingSource bs = new BindingSource();
            bs.DataSource = Orders;
            // Bind DataGrid to Binding Source.
            dataGridView1.DataSource = bs;

            // Create Product Table

            DataTable productTable = new DataTable();

            productTable.Columns.Add("Product_ID");
            productTable.Columns.Add("Product_Description");

            DataRow rw1 = productTable.NewRow();

            rw1["Product_ID"] = "23";
            rw1["Product_Description"] = "Pantera Home Videos";

            DataRow rw2 = productTable.NewRow();

            rw2["Product_ID"] = "24";
            rw2["Product_Description"] = "Muse Videos";

            DataRow rw3 = productTable.NewRow();

            rw3["Product_ID"] = "25";
            rw3["Product_Description"] = "Megadeth Videos";

            productTable.Rows.Add(rw1);
            productTable.Rows.Add(rw2);
            productTable.Rows.Add(rw3);

            // Create ComboBoxColum

            DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();

            // Set Its Datasource for the CombBox  to the Product Table
            combo.DataSource = productTable;
            combo.HeaderText = "Product_ID";

            // CHoose Display Member.
            combo.DisplayMember = "Product_ID";

            // Set the DataProperty to the Existing column.
            combo.DataPropertyName = "Product_ID";

            // Add ComboBocColumn to DataGrid.
            dataGridView1.Columns.Add(combo);

            // Hide the Bound Product_ID Column from the Orders Table
            dataGridView1.Columns[1].Visible = false;

        }

        private void button1_Click(object sender, EventArgs e)
        {

           // Record to XML to test changes in DataGrid are effecting the Bound Orders table.
           Orders.WriteXml(@"c:\Test\Output.xml");
        }
于 2012-11-13T18:32:52.403 回答