C# 在 datagridview 中下拉组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19042962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:58:25  来源:igfitidea点击:

Drop down Combobox inside datagridview

c#winformsdatagridviewcombobox

提问by user2793090

I've been struggling with this problem for too long now, I've seen all the answers on the subject and though I've found several none of them seems to work for me. So the base of my problem is as follows: I have a DataGridViewthat will add a row to itself once another DataGridViewcell is double clicked. When this DataGridViewgets the row added, it adds 2 types of columns to itself one is a ComboBox, wich suposedly has a colection already set in it (just went to the ComboBoxoptions inside the datagrid and filled up its collection) and a check box column, now both of them do nothing once i click on them, doble clic, multiple click as many clics as u want but nothing happens. Ive even tryed the following code.

我一直在为这个问题苦苦挣扎太久了,我已经看到了有关该主题的所有答案,尽管我发现有几个答案似乎对我不起作用。所以我的问题的基础如下:我有一个DataGridView会在另一个DataGridView单元格被双击后向自身添加一行。当DataGridView添加行时,它会向自身添加两种类型的列,一种是 a ComboBox,据说已经在其中设置了一个集合(只需转到ComboBox数据网格中的选项并填写其集合)和一个复选框列,现在一旦我点击它们,它们都什么也不做,doble clic,多次点击你想要的尽可能多的clic,但没有任何反应。我什至尝试了以下代码。

public static void combolist(DataGridView combogrid)            
{

    var column = new DataGridViewComboBoxColumn();

    DataTable data = new DataTable();

    data.Columns.Add(new DataColumn("Value", typeof(string)));
    data.Columns.Add(new DataColumn("Description", typeof(string)));

    data.Rows.Add("item1");
    data.Rows.Add("item2");
    data.Rows.Add("item3");

    column.DataSource = data;
    column.ValueMember = "Value";
    column.DisplayMember = "Description";

    combogrid.Columns.Add(column); 
}

and even though I can add a new column of the type ComboBoxto my DataGridViewit is still empty(or appears to be since I can't click to see a drop down list). my data DataGridViewgridview properties are set to:

即使我可以ComboBox向我的类型添加一个新列,DataGridView它仍然是空的(或者似乎是因为我无法单击以查看下拉列表)。我的数据DataGridViewgridview 属性设置为:

editMode:editOnEnter, readOnly:false.

编辑模式:editOnEnter,只读:假。

Is there something I'm missing here? why cant I populate or display this ComboBox?, plz this problem is driving me crazy, and I believe this is the best site to find an answer. I would really much appreciate it... a lot.

有什么我在这里想念的吗?为什么我不能填充或显示这个ComboBox?,请这个问题让我发疯,我相信这是找到答案的最佳网站。我真的很感激它......很多。

Ok so I definitely need to see the problem from another perspective, I've even tried binding the ComboBoxto a datasource and still doesn't display anything!, even though the same datasource binded to a normal ComboBoxgets the desired result

好的,所以我肯定需要从另一个角度看待问题,我什至尝试将 绑定ComboBox到数据源,但仍然没有显示任何内容!,即使绑定到普通数据源的相同数据源ComboBox获得了所需的结果

DataGridViewComboBoxCell ComboColumn = (DataGridViewComboBoxCell)(combogrid.Rows[0].Cells[2]);

ComboColumn.DataSource = class.details.GetData();
ComboColumn.DisplayMember = "name";

is there some basic step I'm missing when working with ComboBoxinside DataGridView?

ComboBox在内部工作时,我是否遗漏了一些基本步骤DataGridView

采纳答案by Onsokumaru

Some thoughts:

一些想法:

  1. I've tested your code as posted with visual studio 2012 and executing the code by click on a Buttonhaving an empty DataGridView. It worked for me, because I got a DataGridComboBoxColumnhaving a ComboBoxwith three empty entries. I extended the code as below and got three named entries:

        data.Columns.Add(new DataColumn("Value", typeof(string)));
        data.Columns.Add(new DataColumn("Description", typeof(string)));
    
        data.Rows.Add("item1");
        data.Rows[data.Rows.Count - 1].SetField("Value", "value1");
        data.Rows[data.Rows.Count - 1].SetField("Description", "description1");
        data.Rows.Add("item2");
        data.Rows[data.Rows.Count - 1].SetField("Value", "value2");
        data.Rows[data.Rows.Count - 1].SetField("Description", "description2");
        data.Rows.Add("item3");
        data.Rows[data.Rows.Count - 1].SetField("Value", "value3");
        data.Rows[data.Rows.Count - 1].SetField("Description", "description3");
    
        column.DataSource = data;
    

    It seems that your code just adds a row named "item1/2/3" or a row that just got the first column ("Value" which is not your displayvalue) filled with the given value to the datatable having no values which could be displayed. In the end I couldn't reproduce your problem of not being able to open the dropdownlist (notice that I used a empty DataGridViewbecause I don't know what else you have in your DataGridView).

  2. I don't know if the posted code is exactly that code where you have trouble with. But you write that you add a row to your DataGridViewwhen a cell is clicked and that when this happen you add two columns. Do you mean you add for each added row two columns to the grid or do you just mean that the row consists of this two columns?
    If the last one is right it seems to me to be a similar problem as with the posted code. Did you consider to create the row manually with adding a DataGridViewComboBoxCelland DataGridViewCheckboxCell? We have a quiet complicated DataGridViewUserControlwhere we dynamically add different celltypes and have no problems with getting them displayed correctly. But we do create all Rows manually, filling the wished cell-types by hand into the rows and do not specify a type for a column.

  1. 我已经测试了您在 Visual Studio 2012 中发布的代码,并通过单击Button具有空的DataGridView. 它对我有用,因为我得到了DataGridComboBoxColumn一个ComboBox包含三个空条目的 a 。我扩展了如下代码并获得了三个命名条目:

        data.Columns.Add(new DataColumn("Value", typeof(string)));
        data.Columns.Add(new DataColumn("Description", typeof(string)));
    
        data.Rows.Add("item1");
        data.Rows[data.Rows.Count - 1].SetField("Value", "value1");
        data.Rows[data.Rows.Count - 1].SetField("Description", "description1");
        data.Rows.Add("item2");
        data.Rows[data.Rows.Count - 1].SetField("Value", "value2");
        data.Rows[data.Rows.Count - 1].SetField("Description", "description2");
        data.Rows.Add("item3");
        data.Rows[data.Rows.Count - 1].SetField("Value", "value3");
        data.Rows[data.Rows.Count - 1].SetField("Description", "description3");
    
        column.DataSource = data;
    

    似乎您的代码只是添加了一个名为“item1/2/3”的行或一个刚将第一列(“Value”,不是您的显示值)填充给数据表的行,该列没有任何值显示。最后,我无法重现您无法打开下拉列表的问题(请注意,我使用了一个空的,DataGridView因为我不知道您的DataGridView.

  2. 我不知道发布的代码是否正是您遇到问题的代码。但是您写道,DataGridView当单击单元格时向您添加一行,并且在发生这种情况时添加两列。您的意思是为每个添加的行添加两列到网格中,还是只是表示该行由这两列组成?
    如果最后一个是正确的,在我看来,这与发布的代码存在类似的问题。您是否考虑通过添加DataGridViewComboBoxCell和手动创建行DataGridViewCheckboxCell?我们有一个安静的复杂功能DataGridViewUserControl,我们可以动态添加不同的单元格类型,并且在正确显示它们方面没有问题。但是我们确实手动创建了所有行,手动将所需的单元格类型填充到行中,并且不为列指定类型。

回答by King King

It's because your DataTablehas 2 columns. But you add only values for cells at column 1, the cells at column 2 (which will be used as DisplayMemberof your comboBox) have empty values. Try this instead:

这是因为你DataTable2 columns。但是您只为 column1中的单元格添加值,第 2 列(将在DisplayMember您的 中使用comboBox)的单元格具有空值。试试这个:

//.....
data.Rows.Add("item1","This is Item1");
data.Rows.Add("item2","This is Item2");
data.Rows.Add("item3","This is Item3");
//.....

回答by ssug89

You are adding 2 columns to the datatable, and filling up only the value member. Try this

您正在向数据表中添加 2 列,并仅填充值成员。尝试这个

        var column = new DataGridViewComboBoxColumn();

        DataTable data = new DataTable();

        data.Columns.Add(new DataColumn("Value", typeof(string)));
        data.Columns.Add(new DataColumn("Description", typeof(string)));

        data.Rows.Add("item1","123");
        data.Rows.Add("item2","234");
        data.Rows.Add("item3","245");

        column.DataSource = data;
        column.ValueMember = "Value";
        column.DisplayMember = "Description";

        dataGridView1.Columns.Add(column);