先说一下客户的需求,有点古怪
就是在某一列中,放一个类似的CheckBoxList的控件那样,可以多选的东东,选择几项保存的时候就插几条记录进数据库,其余的字段内容一样。
但是图上这个有CheckBoxList那行是Add上去的(Grid.DisplayLayout.Bands[0].AddNew();) 在点Save的时候是Insert 操作
原有的行则是从数据库出来的,在点击Save的时候则是Update操作,如下图,要求是下拉框了,因为Update的时候,只能保存成另外一个值,不可以让用户去多选
本来这样的形式在一样情况下可以正常出效果的(同一列不同单元格放不同的Editor),在Grid的InitializeRow事件,和在UltraGridRow row = Grid.DisplayLayout.Bands[0].AddNew();中对它们的Cell的EditorComponent进行设置是没有什么问题的,但是奇怪的是在当前这种情况下确出了问题,把上面保存操作的行上dropdownlist 绑定好,下面插入的新行绑定显示的checkboxlist多选的下拉框确出不来了!好像是互相排斥一样。在试了很多方法,又google了很多网页,包括官方社区以后。自己试出来要的图上的效果了。
说一下要点
1. 要绑定的那个CheckBoxList东东,不要动态创建,比如UltraComboEditor comboEditor = new UltraComboEditor ();
在设计界面拖一下UltraComboEditor 放在界面上Visable设成false
2.在绑定完Grid之前,直接设置列的编辑控件为UltraComboEditor
Grid.DisplayLayout.Bands[0].Columns["QualifierID"].EditorComponent = comboEditor;
3.在InitializeRow事件中再分别绑定
private void SetEditControlToRow(UltraGridRow row) { foreach (UltraGridCell gridCell in row.Cells) { switch (gridCell.Column.Key) { case "QualifierID": if (!string.IsNullOrEmpty(gridCell.Row.Cells["AllocationID"].Value + "")) { UltraCombo dropDown5 = new UltraCombo(); dropDown5.DataSource = _presenter.GetQualifierType(); dropDown5.DisplayMember = "QualifierType"; dropDown5.ValueMember = "QualifierID"; dropDown5.DisplayLayout.Bands[0].HeaderVisible = false; dropDown5.DisplayLayout.Bands[0].Columns[1].Hidden = true; gridCell.EditorComponent = dropDown5; dropDown5.RowSelected += new RowSelectedEventHandler(dropDown5_RowSelected); SetValueOnCell(gridCell, dropDown5); Grid.DisplayLayout.Bands[0].Columns["SubInventoryId"].Hidden = gridCell.Row.Cells["QualifierID"].Text.ToUpper() != "SUBINVENTORY"; } else { comboEditor.DataSource = _presenter.GetQualifierType(); comboEditor.DisplayMember = "QualifierType"; comboEditor.ValueMember = "QualifierID"; comboEditor.CheckedListSettings.CheckBoxAlignment = ContentAlignment.MiddleRight; comboEditor.CheckedListSettings.CheckBoxStyle = CheckStyle.CheckBox; comboEditor.CheckedListSettings.EditorValueSource = EditorWithComboValueSource.CheckedItems; comboEditor.CheckedListSettings.ListSeparator = ","; comboEditor.CheckedListSettings.ItemCheckArea = ItemCheckArea.Item; gridCell.EditorComponent = comboEditor; } break; } } }上面代码以行中有无主键ID为判断,有就是Update放上普通控件,无就是Insert设置成CheckBox下拉框。combEditor就是我拖到界面上的那个UltraComboEditor.
重点解释一下下面这几行代码
comboEditor.CheckedListSettings.CheckBoxAlignment = ContentAlignment.MiddleRight; comboEditor.CheckedListSettings.CheckBoxStyle = CheckStyle.CheckBox; comboEditor.CheckedListSettings.EditorValueSource = EditorWithComboValueSource.CheckedItems; comboEditor.CheckedListSettings.ListSeparator = ","; comboEditor.CheckedListSettings.ItemCheckArea = ItemCheckArea.Item;comboEditor.CheckedListSettings.CheckBoxAlignment = ContentAlignment.MiddleRight;checkbox对齐方式comboEditor.CheckedListSettings.CheckBoxStyle = CheckStyle.CheckBox; 显示为CheckBox,也可以设置成三态TriState,None等,这里需要多选, 所以就CheckBoxcomboEditor.CheckedListSettings.EditorValueSource = EditorWithComboValueSource.CheckedItems; 设置该Editor为选中的Item值,也可以设置为SelectedItem,这样只点击那一项,不用打勾也行 comboEditor.CheckedListSettings.ItemCheckArea = ItemCheckArea.Item;设置点击点为选中的区域,Item是点击那一项,Checkbox为为CheckBox打勾才算选中