1
我有一個datagridview其中有2個datagridviewcomboboxcolumns。我通過編輯控件展示方法設置了selectedindexchanged方法(因爲我已經讀過它,應該這樣做) 但是,由於某種原因,當第一次更改爲第二個組合框時,此事件觸發。SelectedIndexChanged事件也發射錯誤datagridviewcomboboxcell
我的問題是;是什麼導致這種方法觸發?我明確檢查它是分配處理程序之前的第一列,但我仍然需要檢查處理程序本身,因爲至少在一次出現columnindex是1.
任何幫助將不勝感激。讓我知道,如果我什麼都不清楚。
private void AddLicenses_Load(object sender, EventArgs e)
{
this._data = this.GetData();
DataGridViewComboBoxColumn productColumn = new DataGridViewComboBoxColumn();
productColumn.DataPropertyName = "Name";
productColumn.HeaderText = "Product";
productColumn.Width = 120;
productColumn.DataSource = this._data.Select(p => p.Name).Distinct().ToList();
this.licenses.Columns.Add(productColumn);
DataGridViewComboBoxColumn distributorColumn = new DataGridViewComboBoxColumn();
distributorColumn.DataPropertyName = "Distributor";
distributorColumn.HeaderText = "Distributor";
distributorColumn.Width = 120;
this.licenses.Columns.Add(distributorColumn);
}
private void licenses_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox productDropDown = (ComboBox)e.Control;
if (productDropDown != null && this.licenses.CurrentCell.ColumnIndex == 0)
{
productDropDown.SelectedIndexChanged -= productDropDown_SelectedIndexChanged;
productDropDown.SelectedIndexChanged += productDropDown_SelectedIndexChanged;
}
}
private void productDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.licenses.CurrentCell.ColumnIndex == 0)
{
ComboBox productDropDown = (ComboBox)sender;
DataGridViewComboBoxCell distributorCell = (DataGridViewComboBoxCell)this.licenses.CurrentRow.Cells[1];
distributorCell.Value = null;
distributorCell.DataSource = this._data.Where(p => p.Name == (string)productDropDown.SelectedValue).OrderBy(p => p.UnitPrice).Select(d => new EnLicense() { Distributor = d.Distributor, UnitPrice = d.UnitPrice }).ToList();
distributorCell.ValueMember = "Distributor";
distributorCell.DisplayMember = "DistributorDisplay";
}
}