KB Article A1402
Visible to All Users

Can SelectedIndex be set for RepositoryItemComboBox?

Description:
Why does the RepositoryItemComboBox not publish the SelectedIndex and SelectedItem properties? How do I programatically select an item in the RepositoryItemComboBox?

Answer:
Applies to:
XtraGrid, XtraVerticalGrid, XtraTreeList, XtraBars
A RepositoryItem is just a template. It does not store a value. This applies to all repository item classes: RepositoryItemLookUpEdit, RepositoryItemTextEdit, RepositoryItemCheckEdit, etc. You should either work with the actual editor (BaseEdit) or with the editor container (e.g. GridView or BarManager) to get/set editor values.
In your case, you can use the ActiveEditor property of your editor container and typecast it to ComboBoxEdit. Then set its SelectedIndex property. This approach is common for all our controls, which use in-place editors: XtraGrid, XtraVerticalGrid, XtraTreeList and XtraBars. Please note that it works only if the in-place editor is activated, otherwise the ActiveEditor property is null (Nothing). That is, a grid cell must be open for editing or the editor on the XtraBars toolbar must be focused.
The recommended approach is to set a value directly to your editor container:
XtraGrid: use the GridView.SetRowCellValue method or change the column value in the grid's linked data table.
XtraBars: set the BarEditItem.EditValue property.
XtraTreeList: use the**TreeList.SetRowCellValue**method.
XtraVerticalGrid: use the VGridControl.SetCellValue method.

C#
// Get the value at index 1 const int index = 1; object val = repositoryItemComboBox1.Items[index]; // stand-alone editor //comboBoxEdit1.SelectedIndex = index; // or comboBoxEdit1.EditValue = val; // XtraGrid gridView1.SetRowCellValue(gridView1.FocusedRowHandle, "Day", val); // XtraBars barEditItem1.EditValue = val;
Visual Basic
' Get the value at index 1 Const index As Integer = 1 Dim value = repositoryItemComboBox1.Items(index) ' stand-alone editor 'comboBoxEdit1.SelectedIndex = index ' or comboBoxEdit1.EditValue = value ' XtraGrid gridView1.SetRowCellValue(gridView1.FocusedRowHandle, "Day", value) ' XtraBars BarEditItem1.EditValue = value

Attached is an example, illustrating this approach.
Please note that an in-place editor's value is posted only at the moment when the former loses focus or when you press the Enter key.

For example, you type a value in a grid cell. Then, if you press the Enter key, the new value will be posted to a datasource. Until you do this, the underlying datasource contains an old value. This allows you to press the ESC key to cancel the changes. The same behavior is present in all our containers. So, the way and method you would need to use to obtain the current value depends on your scenario. If, for example, you need to immediately update a label during the editing of the value within BarEditItem, handle the corresponding RepositoryItem's EditValueChanged event, cast the sender parameter to the BaseEdit type, and use its EditValue property. (The sender object is the same object that is returned by the BarManager.ActiveEditor property). If you need to catch the moment when the value of BarEditItem itself is changed, handle the BarEditItem.EditValueChanged event and use the item's EditValue property.

To summarize: first, you need to decide whether you need to catch changes made in the in-place editor or its container. Then, handle events and use properties of a corresponding object. For in-place editors, use RepositoryItem's events and methods. For containers, use the containers' events and methods.
See Also:
Repositories and Repository Items
Modify and Validate Cell Values

Comments (3)

    Still I don't understand - once the comboBox has been created, how do I set it to show a specific value from the list? I do not see a "selectedItem" property.

    DevExpress Support Team 10 years ago

      Hello,

      To process your recent post more efficiently, I created a separate ticket on your behalf: T171836: How to select an item in ComboBoxEdit. This ticket is currently in our processing queue. Our team will address it as soon as we have any updates.

      D D
      Dev2-Account Proman 9 years ago

        @Dafna F:

        Code
        int yourIndex = 0;
        Code
        ComboBoxEdit editor = gridView1.ActiveEditor as ComboBoxEdit; if (editor != null) editor.SelectedIndex = yourIndex;

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.