更改 DataGridView 中 ComboBoxColumn 的选择

发布于 2024-12-16 01:15:26 字数 437 浏览 7 评论 0原文

我有一个包含不同列的 dataGridView 。其中之一是具有默认选择(英语、德语、中文...)的 ComboBoxColumn。我以编程方式将新行添加到我的 datagridview 中。

dataGridView1.Rows.Add(sn, givenName, mail, department, ToDo);

第五列是我的 ComboBoxColumn,当​​前写为“ToDo”。 我想说应该选择我的comboBoxItems 中的哪一个。例如这样:

dataGridView1.Rows.Add(sn, givenName, mail, department, 1);

现在应该在我的组合框中选择德语。我在 Form1.designer.cs 中设置了项目。

稍后我想获取为每行选择的项目的值。

I have a dataGridView with different columns. One of them is a ComboBoxColumn with default selections (English, German, Chinese...). I add new Rows programmatically to my datagridview.

dataGridView1.Rows.Add(sn, givenName, mail, department, ToDo);

the fifth column is my ComboBoxColumn where currently is written "ToDo".
I would like to say which of my comboBoxItems should be selected. For example like this:

dataGridView1.Rows.Add(sn, givenName, mail, department, 1);

Now should be German selected in my comboBox. I set the Items at Form1.designer.cs.

Later on I would like to get the values which Item is selected for each Row.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海的爱人是光 2024-12-23 01:15:26

您应该可以说:

// Assuming your combo box column is named 'comboBoxColumn1'
dataGridView1.Rows.Add(sn, givenName, mail, department, comboBoxColumn1.Items[1]);

请参阅此 MSDN 页面 设置 Items 的示例

You should be able to say:

// Assuming your combo box column is named 'comboBoxColumn1'
dataGridView1.Rows.Add(sn, givenName, mail, department, comboBoxColumn1.Items[1]);

See this MSDN page for an example of setting the Items

独自←快乐 2024-12-23 01:15:26
foreach (DataGridViewRow row in dataGridView1.Rows)
{
      DataGridViewComboBoxCell cell = row.Cells[0] as DataGridViewComboBoxCell;
}

现在您可以从单元格变量访问您的值。

编辑:
当然,Cells[] 中的数字 0 不适合您的示例。

foreach (DataGridViewRow row in dataGridView1.Rows)
{
      DataGridViewComboBoxCell cell = row.Cells[0] as DataGridViewComboBoxCell;
}

now you can access you values from cell variable.

EDIT:
Ofcourse number 0 in Cells[] is not fitting to your example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文