views:

22

answers:

0

I'm writing my own control - a datagridviewcolumn that displays integer values as texts like the comboboxcolumn can but without showing the combobox unless the cell is edited.

I'm mostly there but I have problems with the databinding. I managed to get the necessary properties to appear in the designer but every time I set the datasource and close the editor the changes are dropped.

When I assign the same datasource later in code it works like a charm, I just would prefer not having to do that...

public class DataGridViewLookupColumn : DataGridViewColumn
{
    private DataGridViewLookupCell template;
    private Object datasource = null;
    private String displaymember = String.Empty;
    private String valuemember = String.Empty;
    private BindingSource bindingsource = new BindingSource();

    public DataGridViewLookupColumn()
        : base()
    {
        this.template = new DataGridViewLookupCell();
    }

    public override DataGridViewCell CellTemplate
    {
        get
        {
            return this.template;
        }
        set
        {

        }
    }

    [Category("Data")]
    [
        DefaultValue(null),
        RefreshProperties(RefreshProperties.Repaint),
        AttributeProvider(typeof(IListSource)),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
    ]
    public object DataSource
    {
        get
        {
            return this.bindingsource.DataSource;
            //return this.datasource;
        }
        set
        {
            this.bindingsource.DataSource = value;
            this.bindingsource.EndEdit();
        }
    }

    [Category("Data")]
    [
        DefaultValue(""),
        TypeConverterAttribute("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design"),
        Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor)),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
    ]
    public String DisplayMember
    {
        get
        {
            return this.displaymember;
        }
        set
        {
            this.displaymember = value;
        }
    }

    [Category("Data")]
    [
        DefaultValue(""),
        TypeConverterAttribute("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design"),
        Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor)),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
    ]
    public String ValueMember
    {
        get
        {
            return this.valuemember;
        }
        set
        {
            this.valuemember = value;
        }
    }
}

EDIT: I experimenting I just found out that that original DataGridViewComboBoxColumn can be made to behave exactly like I wanted to. By setting the DisplayStyle to Nothing the combobox control is only shown in edit mode.