单击时,C#DataGridviewComboBoxColumn不执行任何操作

时间:2014-04-09 19:34:36

标签: c# winforms datagridview

我对这里发生的事情非常困惑。我有一个DataGridviewComboBoxColumn我希望像组合框一样(显然)。我有以下代码:

designer.cs中的代码:

this.PurposeCol.DataPropertyName = "Purpose";
this.PurposeCol.DisplayStyle = System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox;
this.PurposeCol.HeaderText = "Purpose";
this.PurposeCol.Name = "PurposeCol";
this.PurposeCol.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.PurposeCol.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.PurposeCol.Width = 78;

表单页面上的构造函数:

PurposeCol.ReadOnly = false;
PurposeCol.DataSource = tripPurposeComboBox.Items;  //Verified that this line fills the datasource with 14 items          
PurposeCol.DisplayMember = "ItemText";
PurposeCol.ValueMember = "ItemValue";

问题在于,当我点击它时没有任何反应。显示的文字是我所期待的,我可以确认DataSource中有14个项目,但我似乎无法显示任何其他项目。是否有特殊设置需要在DataGridviewComboBoxColumn之前设置为ComboBox

2 个答案:

答案 0 :(得分:1)

您为该列设置ReadOnly属性为False,但请确保DataGridView.ReadOnly属性也设置为False (通常默认情况下) )

dataGridView1.ReadOnly = false;

如果设置为True,它将覆盖列上的ReadOnly属性,您将无法打开下拉列表。

答案 1 :(得分:0)

要将其用作可编辑的组合框,您需要在编辑控件显示时实现组合框单元的验证事件 例如。

 private void datagrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (datagrid.CurrentCell.ColumnIndex == 4)
        {
            if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
            {
                DataGridViewComboBoxEditingControl cbo = e.Control as DataGridViewComboBoxEditingControl;
                cbo.DropDownStyle = ComboBoxStyle.DropDown;

                cbo.Validating += new CancelEventHandler(cbo_Validating);
                cbo.SelectedIndexChanged += new EventHandler(SpacingComBox_SelectedIndexChanged);
            }

        }
    }

并实现cbo_Validating和SpacingComBox_SelectedIndexChanged

  

void cbo_Validating(object sender,CancelEventArgs e)private void   SpacingComBox_SelectedIndexChanged(object sender,EventArgs e)