0

I need to Bind a DataGridView to a Data Table that I will populate through Oledb.

This part isn't a problem, but what I want to do is then replace some of the standard columns of the DataGridView and replace them with DataGridViewComboBox columns.

These Columns will then have their own datasource, which will allow the end user to change the default data value, to one of those in the Item Collection.

Has anyone got any decent Links or Tutorials for this kind of Task?

Any help would be hugely appreciated.

Derek
  • 8,300
  • 12
  • 56
  • 88

1 Answers1

0

I managed to get a test working. I hope this helps other people that look at this thread needing help. Its a very basic test. But it works.

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");
        }
Derek
  • 8,300
  • 12
  • 56
  • 88