Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
190
How do I change the displayed text of a cell when the value is not in the databound collection?
posted

I have a simple scenario and need help figuring out how to do this.  I have a table that contains two fields: ID and Name.  Here is what's in the table:

ID: 0, Name: "John"

ID: 1, Name: "Harry"

I have bound this collection into an UltraCombo's DataSource property, with DisplayMember = "Name" and ValueMember = "ID".

I then set an UltraGridColumn's EditorControl to be equal to this UltraCombo.

When the UltraGrid is displayed, it correctly displays "John" if the cell value is 0, and "Harry" if the cell value is 1. 

However, it will display "2" if the cell value is 2, because 2 is not a value in the collection.  It will display "12345" when the cell value is 12345, because 12345 is also not in the collection.

What I want it to do is instead of displaying "2", or "3", or any ID that is not in the collection, I instead want it to display "INACTIVE USER".  How can I do this?

 

 

  • 469350
    Verified Answer
    Offline posted

    Hi,

    Out of curiosity, is there any particular reason you are using an UltraCombo for this? Unless you need multi-select or EditorButtons or a DataFilter, it would be more efficient to use an UltraDropDown control as the column's ValueList.

    HOWTO:What is the best way to place a DropDown list in a grid cell?

    Anyway, there's no really easy way to do this. You will probably need to use a CreationFilter or a DrawFilter. Iw ould probably go with the CreationFilter, myself, so you can change the text in the cell without having to handle the actual drawing of the text.

    Here's a sample of what the CreationFilter might look like. This code was written for using a ValueList, since that's the more common case. It may not work when using UltraCombo as an editor. In such a case, you would have to change around where I get the ValueListResolved to instead get the EditorControlResolved and cast it into an IValueList.


        public class ItemNotInListIndicatorText_CreationFilter : IUIElementCreationFilter
        {
            #region IUIElementCreationFilter Members

            void IUIElementCreationFilter.AfterCreateChildElements(UIElement parent)
            {
                // See if the element is an EditorWithTextDisplayTextUIElement.
                EditorWithTextDisplayTextUIElement editorWithTextDisplayTextUIElement = parent as EditorWithTextDisplayTextUIElement;
                if (editorWithTextDisplayTextUIElement != null)
                {
                    // Get the associated grid cell.
                    UltraGridCell cell = editorWithTextDisplayTextUIElement.GetContext(typeof(UltraGridCell)) as UltraGridCell;

                    // Get the ValueList from the cell.
                    IValueList valueList = cell.ValueListResolved;
                    if (valueList != null)
                    {
                        // Find an item on the list that matches the value of the cell.
                        int index = -1;
                        valueList.GetText(cell.Value, ref index);

                        // If index is -1, it means no matching item was found.
                        if (index == -1)
                        {
                            // Set the text on the element to some useful display text.
                            editorWithTextDisplayTextUIElement.Text = "INVACTIVE USER";
                        }

                    }
                }
            }

            bool IUIElementCreationFilter.BeforeCreateChildElements(UIElement parent)
            {
                // Do nothing
                return false;
            }

            #endregion
        }

    This won't do anything once the cell goes into edit mode. I'm not sure what you would want to do in that case, since setting the actual edit text to a string would not be able to find a matching ID with witch to update the data source. If you are using DropDownList Style or the column is not editable, then that won't matter.