C# 将项目添加到 DataGridViewComboBoxColumn

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15768403/
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 17:53:02  来源:igfitidea点击:

Add item to DataGridViewComboBoxColumn

c#winformsdatagridviewdatagridviewcombobox

提问by Dennys

I have a DataGridView and I change one column to a DataGridViewComboBoxColumn. And I can edit the Items to put some data in it and it works.

我有一个 DataGridView,我将一列更改为 DataGridViewComboBoxColumn。我可以编辑项目以将一些数据放入其中并且它可以工作。

But I want to put the Items in program, therefore, I try to use dataGridView1.Columns["drop_column"]. But it doesn't have a Items

但是我想将项目放在程序中,因此,我尝试使用 dataGridView1.Columns["drop_column"]。但它没有 Items

I found this article but it's to create a new column but I already have a column Add items to DataGridViewComboBoxColumn in DataGridView during runtime.

我找到了这篇文章,但它是为了创建一个新列,但我已经有一个列Add items to DataGridViewComboBoxColumn in DataGridView during runtime

Update: thanks to Ginosaji and Dona, I add a cast and it works now.

更新:感谢 Ginosaji 和 Dona,我添加了一个演员,现在可以使用了。

((DataGridViewComboBoxColumn)dataGridView1.Columns["drop_column"]).Items.Add("1");
((DataGridViewComboBoxColumn)dataGridView1.Columns["drop_column"]).Items.Add("2");
((DataGridViewComboBoxColumn)dataGridView1.Columns["drop_column"]).Items.Add("3");

回答by Doan Cuong

Here is an example:

下面是一个例子:

DataGridViewComboBoxCell CBCell = new DataGridViewComboBoxCell();

CBCell = DataGridView1.Rows(0).Cells("Your column name");
CBCell.Items.Add("Your item here");