C# combobox in ObjectListView column when enter value

2.2k views Asked by At

I have MainForm with TreeListView from ObjectListView library.

I want to input values in ValueColumn (second column) with different Windows.Forms.Controls components.

The TreeView (name is jsonTreeView) normal displayed all values and it's types. It's based on my own class:

public class DataTreeNode
{
    public string Name { get; set; }
    public DataTreeNodeType Type { get; set; }
    public string Value { get; set; }
    public List<DataTreeNode> Children { get; set; }
}

First column is Name, second is Value and third is Type. I want to create different input controls for values with different types (it saved in my class as string, but when convert to json it's parse like Type value). MainForm

public partial class MainForm : 
{
    //...
    ObjectListView.EditorRegistry.Register(typeof(string), delegate (Object model, OLVColumn column, Object value)
    {
        var node = model as DataTreeNode;
        if(node == null) return new TextBox();
        if (column.Index == 1)
        {
        switch (node.Type)
            {
                //...
                case DataTreeNodeType.Boolean:
                    var cmbbBool = new ComboBox();
                    cmbbBool.Items.Add("False");
                    cmbbBool.Items.Add("True");
                    return cmbbBool;
                case DataTreeNodeType.Str:
                    return new TextBox();
                default:
                    return new TextBox();
            }
        }
        return new TextBox();
    }
    //...
}

Documentation says:

Once the cell editor has been created, it is given the cell’s value via the controls Value property (if it has one and it is writable). If it doesn’t have a writable Value property, its Text property will be set with a text representation of the cells value.

When the user has finished editing the value in the cell, the new value will be written back into the model object (if possible). To get the modified value, the default processing tries to use the Value property again. It that doesn’t work, the Text property will be used instead.

But when i try to set any value with comboBox (this control HAS Text property) return value is null. I tried to add not only strings in combobox, but custom and standart classes - nothing happens. enter image description here

enter image description here How can i do this trick?

1

There are 1 answers

0
murzagurskiy On BEST ANSWER

I found some solution (not so good, but solve the problem).

In source code of ObjectListView library i found BooleanCellEditor class. It's inherit from ComboBox and present the value as Boolean. I copy that code in my solution and change value from bool to string.

OLV Source code :

internal class BooleanCellEditor : ComboBox
{
    public BooleanCellEditor() {
        this.DropDownStyle = ComboBoxStyle.DropDownList;
        this.ValueMember = "Key";

        ArrayList values = new ArrayList();
        values.Add(new ComboBoxItem(false, "False"));
        values.Add(new ComboBoxItem(true, "True"));

        this.DataSource = values;
    }
}

My source code:

public class StringBooleanCellEditor : ComboBox
{
    public StringBooleanCellEditor()
    {
        DropDownStyle = ComboBoxStyle.DropDownList;
        ValueMember = "Key";

        var values = new ArrayList
            {
                new ComboBoxItem("False", "Ложь"),
                new ComboBoxItem("True", "Истина")
            };

        DataSource = values;
    }
}

I renamed class that it name more fit to code.