2012-11-13 22 views
0

我需要將DataGridView綁定到我將通過Oledb填充的數據表。DataGridViewComboBox列Datatsource

這部分不是問題,但我想要做的是,然後替換DataGridView的一些標準列,並將它們替換爲DataGridViewComboBox列。

這些列將有自己的數據源,它允許最終用戶將默認數據值更改爲Item集合中的其中一個。

有沒有人有任何體面的鏈接或教程這種任務?

任何幫助將非常感激。

+1

你可以用這個啓動:http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.aspx –

+0

謝謝,這有助於。 – Derek

回答

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"); 
     } 
相關問題