2011-10-09 44 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的類型。我正在尋找這個主題,所以 我認爲有人可以使用它。