【问题标题】:Why on Initial load DataGridViewComboBoxColumn showing empty value为什么在初始加载 DataGridViewComboBoxColumn 时显示空值
【发布时间】:2012-10-01 08:41:58
【问题描述】:

大家好,我已将我的 datagridview 与 DataGridViewComboBoxColumn 绑定如下

DataGridViewComboBoxColumn clnStatus = new DataGridViewComboBoxColumn();
clnStatus.DataPropertyName = "Status";
clnStatus.AutoComplete = true;
clnStatus.ValueMember = "absent";
clnStatus.Name = "Absent - Leave - Present";
clnStatus.DataSource = new string[] { "absent", "present", "leave" };
dataGridView1.Columns.Insert(0, clnStatus);

但这会导致我的 datagridview 为空值,如下所示。我应该怎么做才能选择一个值

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    没有选择初始值,因为单元格的 Value 属性没有任何值,因此您需要通过遍历网格来设置该值。

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

    【讨论】:

      【解决方案2】:

      使用以下代码行设置值:

      dataGridView1.Rows["RowNumber"].Cells["ColumnNameOrNumber"].Value;
      

      参考:how to get selectedvalue from DataGridViewComboBoxColumn?

      【讨论】:

        【解决方案3】:

        Value 属性对表单加载没有影响。尝试将组合与您的数据表源绑定并设置以下属性,而不是使用静态 ComboBox 项:

        comboBoxColumn.DataPropertyName = "Column_Name";
        

        然后在每个单元格上使用 .Value 以确保每一行都有预期值。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-21
          • 1970-01-01
          • 2021-12-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多