This is a migrated thread and some comments may be shown as answers.

GridViewCheckedDropDownListColumn

1 Answer 74 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 30 Nov 2015, 04:07 PM

I am trying to create a GridViewCheckedDropDownListColumn. I have some of it working.

It is creating the column and the drop-down is displaying as it should. However, the main cell does not show the checked items after the drop-down is no longer displayed. Nor does it remember which items had previously been checked when the drop-down is displayed a second time.

 

I wasn't able to attach my project, so here is the important code:

public class Foo
{
    public int Id { get; set; }
 
    public string Value { get; set; }
 
    public bool IsSelected { get; set; }
}
public class Boo : Foo
{
    public List<Foo> Foos { get; set; }
 
    public Boo()
    {
        this.Foos = new List<Foo>();
    }
}

 

public class RadCheckedDropDownListEditorElement : RadCheckedDropDownListElement
{
    ...
}

public class RadCheckedDropDownListEditor : BaseGridEditor
{
    public string ValueMember { get; set; }
 
    public string DisplayMember { get; set; }
 
    public string CheckedMember { get; set; }
 
    public override object Value
    {
        get
        {
            var listEditorElement = (RadCheckedDropDownListElement) this.EditorElement;
            return listEditorElement.DataSource;
        }
        set
        {
            var listEditorElement = (RadCheckedDropDownListElement) this.EditorElement;
            listEditorElement.ValueMember = this.ValueMember;
            listEditorElement.DisplayMember = this.DisplayMember;
            listEditorElement.CheckedMember = this.CheckedMember;
            listEditorElement.DataSource = value;
        }
    }
     
    [AttributeProvider(typeof(IListSource))]
    [RadDescription("DataSource", typeof(RadListElement))]
    [Category("Data")]
    [RadDefaultValue("DataSource", typeof(RadListElement))]
    public object DataSource
    {
        get
        {
            var listEditorElement = (RadCheckedDropDownListElement)this.EditorElement;
            return listEditorElement.DataSource;
        }
        set
        {
            var listEditorElement = (RadCheckedDropDownListElement)this.EditorElement;
            listEditorElement.DataSource = value;
        }
    }
     
    protected override RadElement CreateEditorElement()
    {
        return new RadCheckedDropDownListEditorElement();
    }
}
public class GridViewCheckedDropDownListColumn : GridViewComboBoxColumn
{
    private RadCheckedDropDownListEditor _editor;
    private string _checkedMember;
 
    [Browsable(true)]
    [Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
    [Category("Data")]
    [DefaultValue(null)]
    [Description("Gets or sets a string that specifies the property or database column from which to get values that correspond to the items in the RadDropDownListEditor.")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public string CheckedMember
    {
        get
        {
            return this._checkedMember;
        }
        set
        {
            if (this._checkedMember == value) return;
            this._checkedMember = value;
            this.DispatchEvent(KnownEvents.ColumnDataSourceInitializing, GridEventType.UI, GridEventDispatchMode.Send, (object)null, (object[])null);
        }
    }
     
    public GridViewCheckedDropDownListColumn()
    {
    }
 
    public GridViewCheckedDropDownListColumn(string fieldName) : base(fieldName)
    {
    }
 
    public GridViewCheckedDropDownListColumn(string uniqueName, string fieldName) : base(uniqueName, fieldName)
    {
    }
 
    public override Type GetDefaultEditorType()
    {
        return typeof(RadCheckedDropDownListEditor);
    }
 
    public override IInputEditor GetDefaultEditor()
    {
        return new RadCheckedDropDownListEditor();
    }
 
    public override void InitializeEditor(IInputEditor editor)
    {
        var dropDownListEditor = (RadCheckedDropDownListEditor) editor;
 
        if (dropDownListEditor == null) return;
 
        dropDownListEditor.ValueMember = this.ValueMember;
        dropDownListEditor.DisplayMember = this.DisplayMember;
        dropDownListEditor.CheckedMember = this.CheckedMember;
        dropDownListEditor.DataSource = this.DataSource;
    }
 
    public virtual void RadGridView_EditorRequired(object sender, EditorRequiredEventArgs e)
    {
        if (e.EditorType == typeof(RadCheckedDropDownListEditor))
        {
            e.Editor = this._editor ?? (this._editor = new RadCheckedDropDownListEditor());
        }
    }
}

 

public partial class Form1 : Form
{
    private List<Boo> _boos;
 
    public Form1()
    {
        this.InitializeComponent();
        this.InitializeList();
        this.InitializeColumns();
        this.radGridView.DataSource = this._boos;
    }
 
    private void InitializeList()
    {
        this._boos = new List<Boo>();
 
        for (int i = 0; i < 10; i++)
        {
            Boo b = new Boo
            {
                Id = i,
                Value = $"Boo-{i}"
            };
 
            for (int j = 0; j < 10; j++)
            {
                Foo f = new Foo
                {
                    Id = j,
                    Value = $"Foo-{i}.{j}"
                };
                b.Foos.Add(f);
            }
 
            this._boos.Add(b);
        }
    }
 
    private void InitializeColumns()
    {
        var c1 = new GridViewTextBoxColumn(nameof(Boo.Id));
        var c2 = new GridViewTextBoxColumn(nameof(Boo.Value));
        var c3 = new GridViewCheckedDropDownListColumn(nameof(Boo.Foos))
        {
            ValueMember = nameof(Foo.Id),
            DisplayMember = nameof(Foo.Value),
            CheckedMember = nameof(Foo.IsSelected),
            Width = 500
        };
 
        this.radGridView.Columns.Add(c1);
        this.radGridView.Columns.Add(c2);
        this.radGridView.Columns.Add(c3);
 
        this.radGridView.EditorRequired += c3.RadGridView_EditorRequired;
    }
}

1 Answer, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 03 Dec 2015, 06:26 AM
Hi Paul,

Thank you for writing.

Please check the attached project for an example how this can be achieved. You would only need to create a custom editor as a descendant of RadCheckedDropDownListElement. You can also find the example in our documentation: http://www.telerik.com/help/winforms/dropdown-and-listcontrol-checked-dropdownlist-how-to-use-as-radgridview-editor.html

I am also sending you a short video showing the result on my end. In case you need additional information or the solution does not fit your scenario please let me know.

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
Paul
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Share this question
or