Hi All,
I have a wingrid within my application, one column of which has its style set as a checkbox.
The exact scenario for the checkbox is as follows:
1. The grid has a column for which the style is set as a checkbox.
2. When the form loads the cell should display N.
3. When we click on the cell the checkbox is displayed. On checking and tabbing out Y should be displayed.
On unchecking and tabbing out N should be displayed.
I have made use of a datafilter class to achieve the same but the problem is that when the form loads control does not come to the datafilter class, ie i have no control over the first value which is shown on the cell. Once i click on the cell and then tabout it seems to work perfectly. Could you please let me know how i can alter the value being displayed on the cell, when the grid is loading for the first time?
For reference i have attached a sample isolated application having the problem. I have a column "Check" which is of type string and has its style as checkbox.
I have hardcoded true or false values for the column Check. This is because in the actual application the values come from the server and I cant change it. I have attached a datafilter to change the checkbox to return values Y or N. The problem arises when the grid loads, it displays values true or false. I want to know if there is any way at which I can have the grid display Y or N based on whether the value is true or false as soon as the grid loads. I also cant do any kind of hard coding in the form load event because the value actually comes from the server.
Thanks in advance.
Prenil
Hi Prenil,
I'm not sure if that's possible. You might try experimenting with the CellClickAction property on the column to see if that makes any difference.
Mike,
Thanks for the approach. I am able to display Y/N within the cell when the grid initially loads and the chekcbox also appears on clicking on a cell. The only problem which i am facing now is that clicking on a cell with Y/N displayed, directly changes the check state of the checkeditor. What is expected is that the first click on the cell displays the checkbox editor. The second click on the editor changes its state (ie checks it if it is unchecked and vice versa).
I think the above behavior is because as soon as we click on the cell the events are in the following order :
1. The method BeforeCreateChildElements within the creation filter class is called. Since we are in edit mode the TextUIElement is not painted and the checkbox editor is shown.
2. The click is now caught by the checkbox editor (because there is no other element) and thus the check state is changed.
Is there a way in which i can disable the first click on the cell, so that the checkstate is changed only on the second click?
Regards,
You never have to remove the element. You basically just replace the content of the CheckEditorChecBoxUIElement with a Text Element whenever the cell is not in edit mode.
This is actually a bit trickier than I thought at first, so here's some sample code:
public class CheckBoxCreationFilter : IUIElementCreationFilter { #region IUIElementCreationFilter Members void IUIElementCreationFilter.AfterCreateChildElements(UIElement parent) { // Do nothing } bool IUIElementCreationFilter.BeforeCreateChildElements(UIElement parent) { if (parent is CheckEditorCheckBoxUIElement) { // Get the cell. UltraGridCell cell = parent.GetContext(typeof(UltraGridCell)) as UltraGridCell; if (cell == null) return false; // We only want to display text when the cell is not in edit mode. if (cell.IsInEditMode) return false; // See if there is an existing TextUIElement so we can re-use it. TextUIElement textElement = parent.GetDescendant(typeof(TextUIElement)) as TextUIElement; if (textElement == null) textElement = new TextUIElement(parent, cell.Value.ToString()); else textElement.Text = cell.Value.ToString(); // Get rid of any existing child elements. parent.ChildElements.Clear(); // Set up the TextUIElement. textElement.Rect = parent.Rect; textElement.TextHAlign = HAlign.Center; textElement.TextVAlign = VAlign.Middle; // Add it to the parent. parent.ChildElements.Add(textElement); // Return true to prevent the element from creating it's default child elements. return true; } // Do nothing, return false. return false; } #endregion }
Hi Mike,
I have tried to implement the solution provided by you. Within the creation filter i create a TextUIElement and place it within the cell if we are not in the edit mode. On load of the form the Text(Y/N) is displayed correctly, but on clicking the cell i am not obtaining the CheckBox editor. This was because i was clicking on the UIElement and not on the cell, because of which the cell was not entering edit mode.
I tried to handle the ElementClick event of the UIElement and remove the TextUIElement if we were entering the edit mode so that the Checkbox editor would now be visible, but things doesnt seem to work fine. Is there anything else which needs to be done here?
Where do i remove the UIElement which i have placed over the cell so that the Checkbox editor is visible?
What you're doing here is really not a good idea. The only reason that this cell display text at all is because you have set CellDisplayStyle to PlainText. This setting is intended to improve performance in a text cell where you don't need a lot of fancy editor features, so the cell skips using an editor. Since the editor is not being used, the DataFilter cannot be called, and therefore the translation to Y or N doesn't happen. Once you click on the cell and it goes into edit mode, the editor gets used. But as I said, this is really not the intention of this property.
What I would do is leave CellDisplayStyle set to the default, and then use a CreationFilter to show a text element in the cell. This is a bit more complex, especially if you haven't used CreationFilters before, but it's also a lot more reliable. What you would do is watch for the BeforeCreateChildElements phase of the CellUIElement, check to see if the cell is in edit mode. If not, you would create a TextUIElement and set it's text and position it inside the cell, then return true to cancel the default element creation for that cell.
If you want to try the CreationFilter, I recommend checking the Infragistics KnowledgeBase for some articles and samples. And also, get the UIElementViewer Utility, it's a big help. Introducing the Infragistics UIElementViewer Utility