首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataGridViewComboBox列数据源

DataGridViewComboBox列数据源
EN

Stack Overflow用户
提问于 2012-11-13 23:18:47
回答 1查看 410关注 0票数 0

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

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

然后,这些列将拥有自己的数据源,这将允许最终用户将默认数据值更改为Item Collection中的其中一个。

有没有人有关于这类任务的合适的链接或教程?

任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2012-11-14 02:32:52

我设法让一个测试正常工作。我希望这能帮助其他看到这个帖子需要帮助的人。这是一个非常基本的测试。但它是有效的。

代码语言:javascript
复制
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");
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13363395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档