【问题标题】:change selection from ComboBoxColumn in DataGridView从 DataGridView 中的 ComboBoxColumn 更改选择
【发布时间】:2011-11-17 11:21:10
【问题描述】:

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

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

第五列是我的 ComboBoxColumn,目前写着“ToDo”。 我想说应该选择我的哪个comboBoxItems。比如这样:

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

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

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

【问题讨论】:

    标签: c# datagridview selectedindex


    【解决方案1】:

    你应该可以说:

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

    有关设置Items 的示例,请参阅this MSDN page

    【讨论】:

      【解决方案2】:
      foreach (DataGridViewRow row in dataGridView1.Rows)
      {
            DataGridViewComboBoxCell cell = row.Cells[0] as DataGridViewComboBoxCell;
      }
      

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

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

      【讨论】:

        猜你喜欢
        • 2023-03-16
        • 2018-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多