简体   繁体   English

如何使组合框列仅显示未选择的项目?

[英]How to make a combobox column show only unselected items?

It's supposed to be a very common workflow, so I am surprised that neither C# gridview or DevExpress provides an easy way to do it. 它应该是一个非常普通的工作流程,所以令我感到惊讶的是C#gridview或DevExpress都没有提供简便的方法。

Example: 例:
MyColumn is a field of combobox type column. MyColumn是组合框类型列的字段。 When a user tries to edit value in this field, he will have to choose a value in the dropdown list. 当用户尝试在此字段中编辑值时,他将必须在下拉列表中选择一个值。 When the grid has 1 row, he can choose from "value A", "value B" or "value C". 当网格有1行时,他可以从“值A”,“值B”或“值C”中选择。 If the user has chosen "value A" in the 1st row, then in the 2nd row the available values for MyColumn become "value B" and "value C". 如果用户在第一行中选择了“值A”,则在第二行中MyColumn的可用值将变为“值B”和“值C”。

Currently I am doing it in a dirty way with DevExpress , where I calculate the combobox repository (modify RepositoryItemComboBox.Items property) everytime after a delete, insert or modify operation, and in the CustomRowCellEdit event handler I assign the updated repository to the column. 当前,我正在使用DevExpress进行肮脏的操作,在每次删除,插入或修改操作之后,我都会在此计算组合框存储库(修改RepositoryItemComboBox.Items属性),并在CustomRowCellEdit事件处理程序中将更新的存储库分配给该列。 The code is very convoluted and brittle. 该代码非常复杂。

------update------ I just came up with a better example of a use case: Say that the grid asks the user to select his 5 most favorite US states. ------更新------我刚刚提出了一个用例的更好示例:假设网格要求用户选择他的5个最喜欢的美国州。 So the column State is a combobox type column that initially has a list of 50 states. 因此,State列是一个组合框类型的列,最初具有50个状态的列表。 If the user selects CA for the 1st row, then in the 2nd row he will have only 49 states available to select. 如果用户为第一行选择CA,则在第二行中,他将只有49个状态可供选择。 In this case, you always want values to be unique in each row or it would not make sense. 在这种情况下,您始终希望值在每一行中都是唯一的,否则就没有意义。

You might have thought of it; 您可能已经想到了; but just in case if you haven't: 但以防万一您没有:

I believe, rather than trying to change the repository every time, better to actually validate the selected value in the combo (in the onchange event of the combobox) and override the user selection for this item in case it is a duplicate. 我相信,与其尝试每次都更改存储库,不如实际验证组合中的选定值(在组合框的onchange事件中)并覆盖用户对此项目的选择,以防重复(最好是重复)。

Note: Also, changing the repository, I think, will have it's own share of issues; 注意:此外,我认为更改存储库将由它自己承担。 as changing the DataSource after every delete, insert or modify will alter the data in other combo boxes (Obviously, here I am assuming that all combos uses the same Datasource). 因为在每次删除,插入或修改后更改DataSource,都会更改其他组合框中的数据(很显然,在这里我假设所有组合都使用相同的Datasource)。

I've got a solution. 我有一个解决方案。

Handle the CustomRowCellEditForEditing event. 处理CustomRowCellEditForEditing事件。 In the handler, traverse all rows and get all selected items, and remove the selected items from the repositoryItemComboBox1.Items (suppose that's your column editor). 在处理程序中,遍历所有行并获取所有选定项,然后从repositoryItemComboBox1.Items中删除选定项(假设这是您的列编辑器)。
Of course, the logic is a little more complicated because you will also have to change the Items collection when user edits or deletes a row. 当然,逻辑稍微复杂一点,因为在用户编辑或删除行时,您还必须更改Items集合。

Caution: 警告:
CustomRowCellEdit is the wrong way to go. CustomRowCellEdit是错误的方法。 Because everytime RepositoryItemComboBox.Items is changed the event will fire. 因为每次RepositoryItemComboBox.Items都更改,所以事件将触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM