2017-08-03 54 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

我看到组合框中的数据。 –