winforms DataGridViewComboBoxColumn,设置 DataPropertyName 时 ComboBox 不可点击
Posted
技术标签:
【中文标题】winforms DataGridViewComboBoxColumn,设置 DataPropertyName 时 ComboBox 不可点击【英文标题】:winforms DataGridViewComboBoxColumn, ComboBox not clickable when DataPropertyName is set 【发布时间】:2016-05-16 12:31:47 【问题描述】:我有 DataGridView 和 DataGridViewComboBoxColumn 和数据源绑定,当我绑定 DataGridView 的 DataPropertyName 时,ComboBox 变得不可点击。
dgFMS.ReadOnly = false;
正确的 DataPropertyName 与每一行绑定,但我无法从下拉列表中选择
BindingSource _bsCats2 = new BindingSource();
DataGridViewComboBoxColumn catCol = new DataGridViewComboBoxColumn();
_bsCats2.DataSource = CategoryManager.Categories.Select(x => new
Key = x.ParentWithName.ToLowerInvariant(),
Value = x.Id )
.ToList();
catCol.DataSource = _bsCats2;
catCol.DataPropertyName = "catID";
catCol.DisplayMember = "Key";
catCol.ValueMember = "Value";
catCol.Width = 250;
catCol.ReadOnly = false;
dgFMS.Columns.Add(catCol);
dgFMS.ReadOnly = false;
我也在实现 dgFMS_EditingControlShowing
private void dgFMS_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
ComboBox cb = e.Control as ComboBox;
if (cb != null)
cb.DropDownStyle = ComboBoxStyle.DropDown;
cb.SelectedValueChanged -= new EventHandler(CmbCat_SelectedIndexChanged);
cb.SelectedValueChanged += new EventHandler(CmbCat_SelectedIndexChanged);
【问题讨论】:
【参考方案1】:我发现绑定数据有问题
DataPropertyName = "catID" 是只读属性,只有 getter 而没有像这样的 setter
public int catID
get
return CategoryManager.Categories.FirstOrDefault(x => x.Name.Equals(SubCategory, StringComparison.InvariantCultureIgnoreCase)).Id;
通过添加属性的setter解决了这个问题。
【讨论】:
以上是关于winforms DataGridViewComboBoxColumn,设置 DataPropertyName 时 ComboBox 不可点击的主要内容,如果未能解决你的问题,请参考以下文章