3

I'm currently trying to add a ComboBox to a dataGridView.

In the DGV, there are 5 columns: checkbox, string, string, combobox, combobox.

both combobox-columns are configured as datagridviewcomboboxcolumns (via VisualStudio designer). My problem is to add rows.

My current try is: the columns are already defined and I add rows via dataGridView.Rows.Add. For that, I'm using an array of objects. Example:

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

This passes without any errors. But logically, the comboBoxes aren't filled with anything.

I tried setting a datasource to the 4th and 5th cell of a row:

Error...Using ROW.dataGridViewComboBoxCell.Items.Add: Items are not displayed...

filling obj[3] and 4 with a new DGVcomboBoxCell or -Column:

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

Further information: Each column should have the same Items in the comboBoxes. (These are previously loaded via internet, as xml). Setting a dataSource to the two columns destroys the whole DGV (I think because the other colmns don't have a Datasource). In a nutshell: How to add Rows to a DGV which contain comboboxes filled with items?

Sincerely, NoMad

edit: here's some code to solve my problem:

        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); 

First add a row, cast columns, configure them and add them to the row. No Columns need to be previously specified in the designer.

4

3 に答える 3

4

このソリューションを使用して、アイテムを 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に従って、異なるdatasource(string[])を異なる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;
                }

            }
        }
    }    
于 2011-10-09T15:20:28.007 に答える
1

ここで何をしようとしているのかは完全にはわかりませんが、追加する行ごとにコンボボックスから値を選択しようとしているだけであれば、既存のコンボボックスの値を文字列として選択できます。

2 列の 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」はすでにコンボボックスの選択肢になっています。必要な選択肢を名前で参照するだけで、各行に入力できます。必要に応じて、インデックス番号でこれを行うこともできると思います。

お役に立てれば!

于 2011-10-09T15:12:49.823 に答える