2011-10-09 41 views
0

我目前正试图以一个组合框添加到一个DataGridView一个DGV一行。C#添加了的DataGridViewComboBoxCell

在DGV,有5列:复选框,字符串,字符串,组合框,组合框。

两者组合框柱被配置为datagridviewcomboboxcolumns(经由VisualStudio的设计者)。我的问题是添加行。

我现在的尝试是:列已被定义,我通过dataGridView.Rows.Add添加行。 为此,我使用了一个对象数组。例如:

dataGridViewRow row = new dataGridViewRow(); 
object[] obj = new object[5] {true, "str1", "str2", null, null}; 
dataGridView1.Rows.Add(obj); 

这通过没有任何错误。但从逻辑上来说,comboBoxes没有任何东西。

我试过一个数据源设置为4和行的第五单元:

错误...使用ROW.dataGridViewComboBoxCell.Items.Add:项目不显示...

填充的obj [ 3]和4用新DGVcomboBoxCell或-Column:

Error... :The error message says "The dataGridViewComboBoxCell-Value is invalid. 

进一步的信息:每列应在组合框相同的项目。 (这些以前通过互联网加载,如xml)。将dataSource设置为两列会破坏整个DGV(我认为是因为其他colmns没有数据源)。 简而言之:如何将行添加到包含装满项目的组合框的DGV?

真诚, NOMAD

编辑:这里的一些代码来解决我的问题:

 DataGridViewCheckBoxColumn check = new DataGridViewCheckBoxColumn(); 
     check.Name = "Col1"; 
     dataGridView1.Columns.Add(check); 

     dataGridView1.ColumnCount = 3; 
     dataGridView1.Columns[1].Name = "Col2"; 
     dataGridView1.Columns[2].Name = "Col3"; 

     object[] row = new object[] { true, "str1", "str2" }; 
     dataGridView1.Rows.Add(row); 

     DataGridViewComboBoxColumn combo1 = new DataGridViewComboBoxColumn(); 
     DataGridViewComboBoxColumn combo2 = new DataGridViewComboBoxColumn(); 

     combo1.Name = "Col4"; 
     combo1.Items.Add("100x100"); 
     combo1.Items.Add("200x200"); 

     combo2.Name = "Col5"; 
     combo2.Items.Add("option1"); 
     combo2.Items.Add("option2"); 

     dataGridView1.Columns.Add(combo1); 
     dataGridView1.Columns.Add(combo2); 

首先添加行,投栏,配置它们并将它们添加到该行。 设计器中不需要预先指定列。

回答

1

我并不完全清楚你想在这里做什么,但如果你只是想选择从组合框添加的每个行的值,你可以选择现有的组合框的值作为一个字符串。

如果我有一个双列datagridview,这两个都是预先填充的组合框(我通过简单地编辑每列并在集合中添加我的选择来填充我的组合框在datagridview控件本身中),然后我可以执行此操作:

Public Class Form1 
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
     Dim newRow(1) As Object 
     newRow(0) = "Foo" 
     newRow(1) = "Bar" 
     DataGridView1.Rows.Add(newRow) 
    End Sub 
End Class 

所以“Foo”和“Bar”已经是组合框中的选择。我可以通过简单地引用我想要的名称来填充每一行。我想如果我想的话,我也可以通过索引号来做到这一点。

希望这会有所帮助!

3

您可以使用此解决方案将项目添加到datagird视图组合框列

DataSet ds; //class variable 
    public Form1() 
    { 
     InitializeComponent(); 

     ds = new DataSet(); 
     //column 1 (normal textColumn): 
     dataGridView1.Columns.Add("col1", "Column1"); 
     //column 2 (comboBox): 
     DataGridViewComboBoxColumn comboCol = new DataGridViewComboBoxColumn(); 
     comboCol.Name = "cmbColumn"; 
     comboCol.HeaderText = "combobox column"; 
     dataGridView1.Columns.Add(comboCol); 

     //using dataTable for each datasource:    
     for (int i = 0; i < 10; i++) 
     { 
      string text = "item " + i; //data for text 
      int[] data = { 1 * i, 2 * i, 3 * i }; //data for comboBox: 

      //create new dataTable: 
      DataTable table = new DataTable("table" + i); 
      table.Columns.Add("column1", typeof(string)); 

      //fillig rows: 
      foreach (int item in data) 
       table.Rows.Add(item); 

      //add table to dataSet: 
      ds.Tables.Add(table); 

      //creating new row in dgv (text and comboBox): 
      CreateCustomComboBoxDataSouce(i, text, table); 
     } 
    } 

    private void CreateCustomComboBoxDataSouce(int row, string texst, DataTable table) //row index ,and two parameters 
    { 
     dataGridView1.Rows.Add(texst); 
     DataGridViewComboBoxCell comboCell = dataGridView1[1, row] as DataGridViewComboBoxCell; 
     comboCell.DataSource = new BindingSource(table, null); 
     comboCell.DisplayMember = "column1"; //name of column indataTable to display!! 
     comboCell.ValueMember = "column1"; // vlaue if needed 
     //(mostly you used these two propertes like: Name as DisplayMember, and Id as ValueMember) 
    } 

如果以上不工作就来看看下面的解决方案..

您可以通过的DataGridViewComboBoxCell做到这一点。根据单元格的rowIndex,将不同的数据源(字符串[])设置为不同的DataGridViewComboBoxCell。编码是这样的:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 
    { 
     if (e.ColumnIndex == 0) 
     { 
      DataGridViewComboBoxCell combo = this.dataGridView1[0, e.RowIndex] as DataGridViewComboBoxCell; 

       if (e.RowIndex == 0) 
       { 
        //these data will be displayed in comboBox: 
        string[] data= {"item A1", "item B1", "item C1"}; 
        combo.DataSource = data; 
       } 
       if (e.RowIndex == 1) 
       { 
        //these data will be displayed in comboBox: 
        string[] data = {"item A2", "item B2", "item C2"};       
        combo.DataSource = data; 
       } 
       if (e.RowIndex == 2) 
       { 
        //these data will be displayed in comboBox: 
        string[] data = { "item A3", "item B3", "item C3" };       
        combo.DataSource = data; 
       } 

      } 
     } 
    }  
+0

感谢您的帮助,我想出了如何做到这一点更简单: 我刚刚弄错了dataGridView.Columns.Add方法,我想每次添加一行时,新的列被添加到整个DGB也使用Columns.Add方法,所以我使用了Cells.Add。 (出了什么问题) 在添加行后添加列(到一行)是诀窍。 BTW对不起我的英文不好:P – NoMad

+0

我在Linqpad测试你的第一个解决方案,它工作的很好。但是当我在Visual Studio的表单设计器中使用它时,它不会正确显示ComboxCell。这很奇怪。 – zionpi

0

如果你需要保持配置列尝试这样的事:

dataGridView1.Rows.Add(); 
DataGridViewRow tmp = dataGridView1.Rows[dgvMatw.Rows.Count - 1]; 
tmp.Cells[0] = true; 
tmp.Cells[1] = "str1"; 
tmp.Cells[2] = "str2"; 
((DataGridViewComboBoxCell)tmp.Cells[3]).Value = 1; 
((DataGridViewComboBoxCell)tmp.Cells[4]).Value = 2; 

值1和2,其中仅例子。它必须是DataGridViewComboBoxColumn.ValueMember的类型。我正在寻找这个主题,所以 我认为有人可以使用它。