2012-11-13 3 views
0

Oledb를 통해 채울 데이터 테이블에 DataGridView를 바인딩해야합니다.DataGridViewComboBox Column Datatsource

이 부분은 문제가되지 않지만 DataGridView의 표준 열을 바꿔서 DataGridViewComboBox 열로 바꿉니다.

이러한 열은 자체 데이터 원본을 가지므로 최종 사용자가 기본 데이터 값을 항목 컬렉션의 값 중 하나로 변경할 수 있습니다.

아무도 이런 종류의 작업에 대한 괜찮은 링크 또는 자습서 있어요?

도움이 매우 감사 할 것입니다.

+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"); 
     }