C# 使用 dataGridViewComboBoxCell 添加一个 DataGridView 行

3
我正在尝试在dataGridView中添加一个ComboBox。
DGV中有5列:复选框、字符串、字符串、组合框、组合框。
这两个组合框列都已经通过VisualStudio设计器配置为DataGridViewComboBoxColumns。我的问题是如何添加行。
我的当前尝试是:列已经定义好了,我通过dataGridView.Rows.Add来添加行。
为此,我使用了一个对象数组。示例:
dataGridViewRow row = new dataGridViewRow();
object[] obj = new object[5] {true, "str1", "str2", null, null};
dataGridView1.Rows.Add(obj);

这段代码没有出现任何错误。但是从逻辑上讲,comboBoxes中没有填充任何内容。

我尝试将数据源设置为行的第四个和第五个单元格:

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

通过填充obj[3]和obj[4]来创建一个新的DGVcomboBoxCell或-Column:

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

进一步信息:每个列应该在comboBoxes中具有相同的项目(这些项目是之前通过互联网作为xml加载的)。将数据源设置到两个列会破坏整个DGV(我认为是因为其他列没有数据源)。 简而言之:如何向包含填充有项目的comboboxes的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); 

首先添加一行,转换列,配置它们并将它们添加到行中。在设计师中不需要预先指定任何列。

3个回答

4

您可以使用此解决方案将项目添加到数据网格视图组合框列中。

 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设置不同的数据源(string[])。代码如下:

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

            }
        }
    }    

谢谢你的帮助,我弄明白了如何更简单地做到这一点: 我刚刚误用了dataGridView.Columns.Add方法,我以为每当添加一行时,使用Columns.Add方法时会向整个DGB添加一个新列,所以我使用了Cells.Add。(这是错误的) 在添加行后再添加列(到行中)是诀窍。 顺便说一句,对我的糟糕英语感到抱歉:P - NoMad
我在 Linqpad 中测试了你的第一个解决方案,它运行良好。但是当我在 Visual Studio 的表单设计器中使用它时,它无法正确显示 ComboxCell。这很奇怪。 - zionpi
优秀的帖子,帮我解决了我在DataGridView上遇到的问题。 - Dano

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”已经是组合框中的选项。我可以通过名称简单地引用我想要的选项来填充每一行。如果需要,我猜也可以通过索引号来实现。

希望这能帮到你!


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的类型。我正在搜索这个主题,所以我认为有人可能会用到它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接