我需要一个具有GridLookupEdit的XtraGrid控件来输入列值。
到目前为止我做了什么:
1)我已经配置了XtraGrid控制
columns: ID, Name, Number
in-place GridLookUp Editors for Name and Number columns
2)在form Load事件上,我从数据库加载数据,并设置XtraGrid数据源和repositoryItem(..)数据源,valuemember和displaymember
a)数据在XtraGrid中加载,我可以激活单元格,但不能从下拉菜单中选择值(从repositoryItem(..))或手动输入值(我可以在"*"新行中激活单元格,但它有同样的问题-不能从下拉菜单中选择或手动输入值)=>为什么?
b)如果我不为XtraGrid设置数据源,那么两个下拉框都是活动的,我可以使用GridLookupEdit选择值,但是当我退出单元格时,单元格被清除=>我相信这是因为在这种情况下没有XtraGrid。是否设置了数据源?
var model = (from TableA a in _dbE.TableA select new {ID, Name, Number}).ToList();
//if i comment this line out, then i can choose values from GridLookUpEdit
gridControl1.DataSource = model;
repositoryItemNosaukums.DataSource = model;
repositoryItemNosaukums.ValueMember = "ID";
repositoryItemNosaukums.DisplayMember = "Name";
repositoryItemPieteikumaNr.DataSource = model;
repositoryItemPieteikumaNr.ValueMember = "ID";
repositoryItemPieteikumaNr.DisplayMember = "Number";
我错过了什么?
问题是,我的数据源"模型"是一个列表,但它应该是BindingList与属性AllowNew==True和AllowEdit==True。
在DevExpress支持人员询问数据源是否为只读后,我发现了这篇文章,这篇文章终于让我大开眼界:
一个问题在DataGridView: DataGridView似乎只读用户(WinForms)
正确的代码版本
//no projections (select new ...) for IBindingList<T>..
//var model = (from TableA a in _dbE.TableA select new {ID, Name, Number}).ToList();
var model = (from TableA a in _dbE.TableA).ToList();
//convert to BindingList
var bindingModel= new BindingList<TableA>(model);
bindingModel.AllowNew=true;
bindingModel.AllowEdit=true;
//bind BindingList to datagrid
gridControl1.DataSource = bindingModel; //model;
//... no changes to repositoryItem(..) stuff
repositoryItemNosaukums.DataSource = model;
repositoryItemNosaukums.ValueMember = "ID";
repositoryItemNosaukums.DisplayMember = "Name";
repositoryItemPieteikumaNr.DataSource = model;
repositoryItemPieteikumaNr.ValueMember = "ID";
repositoryItemPieteikumaNr.DisplayMember = "Number";