0

I have DataGridView with DataGridViewComboBoxColumn with datasource binding, when I bind DataPropertyName of DataGridView, ComboBox becomes not clickable.

dgFMS.ReadOnly = false;

Correct DataPropertyName is bind with each row but I can not choose from drop-down

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;

I am also implementing dgFMS_EditingControlShowing as well

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);
    }
}
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Kamran Qadir
  • 466
  • 10
  • 20

1 Answers1

0

I have found problem with binding data

DataPropertyName = "catID" was read-only property with only getter without setter like this

public int catID
    {
        get
        {
            return CategoryManager.Categories.FirstOrDefault(x => x.Name.Equals(SubCategory, StringComparison.InvariantCultureIgnoreCase)).Id;
        }
    }  

By adding setter of the property solved the problem.

Kamran Qadir
  • 466
  • 10
  • 20