2017-08-03 30 views
2

我有一個DataGridView的列作爲ComboBox 這是我用來創建我的列,然後更新每個行DataSource的代碼。DataGridViewComboBoxCell數據源空

var dTable = new DataTable(); 
dTable.Columns.Add("Id",typeof(int)); 
dTable.Columns.Add("Desc", typeof(string)); 

for (int i = 0; i < 10; i++) 
{ 
    var dRow = dTable.NewRow(); 

    dRow[0] = i; 
    dRow[1] = "test"; 
    dTable.Rows.Add(dRow); 
} 
dataGridView1.DataSource = dTable; 

//Create the ComboBoxColumn at the end of the grid 
var cmb = new DataGridViewComboBoxColumn(); 
cmb.Name = "ComboCol"; 
cmb.HeaderText = "ComboCol"; 

dataGridView1.Columns.Add(cmb); 

UpdateDataSourceCombo(); 

private void UpdateDataSourceCombo() 
{ 
    for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) 
    { 
     var comboCell = (DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells["ComboCol"]; 
      //Same datasource for every row just for testing 
     comboCell.DataSource = new string[] { "a", "b", "c" }; 
    } 
} 

該列正確創建爲ComboBox但它總是空的。

UPDATE 進一步測試後,我知道只有當我爲整個DGV使用數據源時纔會出現問題。

它工作正常,如果我只需要創建一個列自己:

var test = new DataGridViewTextBoxColumn(); 
     test.Name = "asd"; 
     dataGridView1.Columns.Add(test); 
     dataGridView1.Rows.Add(new DataGridViewRow()); 

     var cmb = new DataGridViewComboBoxColumn(); 
     cmb.Name = "ComboCol"; 
     cmb.HeaderText = "ComboCol"; 

     dataGridView1.Columns.Add(cmb); 

     UpdateDataSourceCombo(); //Same function as the original post 

我的原代碼與數據表到電網的負載的樣本更新。

+0

它在這裏工作得很好。請注意,下拉列表填充後,除非cell.Value在項目列表中,否則不會顯示值!另外:您需要在設置項目的數據源之前設置DGV數據源。順便說一句,爲什麼'我 TaW

+0

不確定更新的內容。在每次設置DGV.DataSource之後,您需要設置ITems,這很有意義,因爲您希望每個數據都具有行依賴的數據列表。 – TaW

回答

0

在我的項目中工作是這樣的: 爲源數組創建一個類。 將DisplayMemberPath設置爲ComboBox列的ValueMemberPath屬性。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

    namespace sof 
{ 
    public partial class Form1 : Form 
    { 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     System.Data.DataTable dTable = new DataTable(); 
     using (System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection("Data Source=.;Initial Catalog=testBase;Integrated security=true")) 
     { 
      using (System.Data.SqlClient.SqlDataAdapter sqlAdp = new System.Data.SqlClient.SqlDataAdapter("SELECT * FROM City WHERE City_Name LIKE 'Chalon%'", sqlConn)) 
      { 
       sqlConn.Open(); 
       sqlAdp.Fill(dTable); 
      } 
     } 

     dataGridView1.DataSource = dTable; 

     //Create the ComboBoxColumn at the end of the grid 
     var cmb = new DataGridViewComboBoxColumn(); 
     cmb.Name = "ComboCol"; 
     cmb.HeaderText = "ComboCol"; 


     cmb.DisplayMember = "Display"; 
     cmb.ValueMember = "Value"; 
     dataGridView1.Columns.Add(cmb); 

     UpdateDataSourceCombo(); 


    } 
    private void UpdateDataSourceCombo() 
    { 
     for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) 
     { 
      var comboCell = (DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells["ComboCol"]; 
      //Same datasource for every row just for testing 
      if (i % 2 == 0) 
       comboCell.DataSource = new customObj[] { new sof.customObj("a", "a"), new sof.customObj("b", "b"), new customObj("c", "c") }; 
      else 
       comboCell.DataSource = new customObj[] { new sof.customObj("1", "1"), new sof.customObj("2", "2"), new customObj("3", "3") }; 
     } 
    } 

} 

class customObj 
{ 
    public string Value { get; set; } 
    public string Display { get; set; } 
    public customObj(string value, string display) 
    { 
     this.Value = value; 
     this.Display = display; 
    } 

} 
} 
+0

這可行,但我需要爲我的每行都有不同的數據源格。正如我在代碼中所說的,我使用相同的數據源來進行測試。 – SilentRage47

+0

您嘗試添加哪種類型的數據?總是一樣的東西?因爲你的代碼在我嘗試時工作。 if(i%2 == 0) comboCell.DataSource = new string [] {「a」,「b」,「c」};其他 comboCell.DataSource = new int [] {4,5,6}; –

+0

我看到組合框中的數據。 –