Binding a list of strings from a class object to a grid view combo box

1 Answer 2305 Views
GridView
Zale
Top achievements
Rank 1
Zale asked on 22 Oct 2021, 04:49 PM

Good afternoon!

 

I have a class object that has several properties. I have a RadGridView that I've created columns in with field names that match the class properties. When I assign the list of class objects as the datasource to the gridview, everything fills in correctly except the list of strings.  Everything shows up great except the combo boxes that have field names that corresponds with List<string> properties in the class object. The list of strings displays as "System.Collections.Generic.List". Each row is a distinct object with a different list of strings. How do i populate the combo boxes in the with the data in the class object's properties that are List<string>?

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 26 Oct 2021, 12:44 PM
Hello, Zale,

According to the provided information, I suppose that you have a similar structure in the defined class: 
    public class MyObject
    {
        public int Id { get; set; }

        public string Title { get; set; }

        public List<string> Items { get; set; }

        public MyObject(int id, string title, List<string> items)
        {
            this.Id = id;
            this.Title = title;
            this.Items = items;
        }
    }

Please correct me if I am wrong.

I have prepared a sample code snippet for your reference:

        public RadForm1()
        {
            InitializeComponent();

            List<MyObject> sourceData = new List<MyObject>();
            for (int i = 0; i < 10; i++)
            {
                sourceData.Add(new MyObject(i,"Item" + i,new List<string>() { "SubItem" + i, "SubItem" + (i + 1), "SubItem" + (i + 2) }));
            }
            this.radGridView1.AutoGenerateColumns = false;
            GridViewDecimalColumn idColumn = new GridViewDecimalColumn("Id");
            idColumn.FieldName = "Id";
            this.radGridView1.Columns.Add(idColumn);
            GridViewTextBoxColumn titleColumn = new GridViewTextBoxColumn("Title");
            titleColumn.FieldName = "Title";
            this.radGridView1.Columns.Add(titleColumn);
            GridViewTextBoxColumn selectedItemColumn = new GridViewTextBoxColumn("SelectedItem");
            this.radGridView1.Columns.Add(selectedItemColumn);

            this.radGridView1.DataSource = sourceData;
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            this.radGridView1.EditorRequired += radGridView1_EditorRequired; 
        }

        private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
        {
            if (this.radGridView1.CurrentColumn.Name == "SelectedItem")
            {
                RadDropDownListEditor editor = new RadDropDownListEditor();
                e.Editor = editor;
             
                MyObject obj = this.radGridView1.CurrentRow.DataBoundItem as MyObject;
                if (obj != null)
                {
                    List<string> items = obj.Items;
                    RadDropDownListEditorElement el = editor.EditorElement as RadDropDownListEditorElement;
                    el.DataSource = items;
                }
            }
        }

The achieved result is illustrated in the below screenshot:

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Remote troubleshooting is now easier with Telerik Fiddler Jam. Get the full context to end-users' issues in just three steps! Start your trial here - https://www.telerik.com/fiddler-jam.
Tags
GridView
Asked by
Zale
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or