【问题标题】:Set selected items for DataGridView ComboBox failed为 DataGridView ComboBox 设置选定项失败
【发布时间】:2011-08-12 07:49:35
【问题描述】:

我对设置/获取内部组合框字段的选定索引感到困惑。

    this.Parameter.DataSource = lambdacat.Dict();
    {
        foreach (DataGridViewRow row in LimView.Rows)
        {
            //( (ComboBox)row.Cells[1] ) ???
        }
    }

是的,这不起作用:)

谢谢

【问题讨论】:

  • 您可以通过设置单元格的 Value 来设置值并使用 FormattedValue 检索相同的值

标签: c# .net winforms


【解决方案1】:

你可以使用

(row.Cells[1] as DataGridViewComboBoxCell).Value == yourvalue;

并将选定的值作为

(row.Cells[1] as DataGridViewComboBoxCell).FormattedValue

设置默认选择值

(row.Cells[1] as DataGridViewComboBoxCell).Value =(row.Cells[1] as DataGridViewComboBoxCell).Items[yourneededindex] 

【讨论】:

  • 我有一个字符串数组,如果你的值是一个字符串它就不起作用:(
  • 如果要添加数组的项目,请在循环数组时使用 (row.Cells[1] as DataGridViewComboBoxCell).Items.Add(arrayval)
  • 我想你的意思是默认选择,所以你可以使用 (row.Cells[1] as DataGridViewComboBoxCell).Value =(row.Cells[1] as DataGridViewComboBoxCell).Items[yourneededindex ]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多