bind image to a databind combobox

4 posts, 2 answers
  1. anh
    anh avatar
    13 posts
    Member since:
    Nov 2010

    Posted 15 Nov 2010 Link to this post

    Hello,

    I am new at this as I am trying to learn and get comfortable with Telerik. I have a winform app with 2 combo boxes, which are bind from a dataset/datatable. And I would like some sample code on how to bind different images to all each item in my combo box 1 and combo box 2.

    After that I would like to code for the selected_index changed of the combo box 1. I would like to re-bind the data of the combo box 2 base on the selection on combo box 1.

    I can do all of that in the normal combo box where I manually typed in all the items to the combo box and then uses the RadListDataItem Collection Editor to add in the images....but I want to do it with code as my combo box 2 changes base on the selection of combo box 1.

    Thanks.

  2. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 15 Nov 2010 Link to this post

    Hello anh,

    A few questions before i can offer a solution:
    Can you use the Q3 version of the telerik controls?
    Do you really want to use the ComboBox control it is an obsolete control, if you don't really want to rewrite all the code you'll write here in a few months i would suggest using the RadDropDownList.
    If you decide to use the RadDropDownList control please close this thread and open a new thread there, and i will post a solution with images there.

    Best Regards,
    Emanuel Varga
    Telerik WinForms MVP
  3. UI for WinForms is Visual Studio 2017 Ready
  4. anh
    anh avatar
    13 posts
    Member since:
    Nov 2010

    Posted 15 Nov 2010 Link to this post

    Hello Emanuel Varga,

    To answer question.

    No, I don't think I can use Q3 version of the telerik controls.

    And please let me clear things up a little bit. What I meant in the above post as for "combo box 1 and combo box2" are actually RadDropDownList. Sorry if I confuse you. When I type the question above. I was referring or have this image of the old Microsoft combo box in my head.

    I am able to bind the RadDropDownListType with the below code. But I don't know how to add the images to each item in it.

    1.radDropDownListType.DataSource = myDataSet.Tables["Type"];
    2.radDropDownListType.DisplayMember = "Type";               
    3.radDropDownListType.ValueMember = "TypeTableName";

  5. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 16 Nov 2010 Link to this post

    Hello again,

    Sorry there is no very easy way of doing this but you can register for the ItemDataBound event and set the image there, like this example:
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    public partial class Form1 : Form
    {
        private RadDropDownList radDropDownList1;
     
        public Form1()
        {
            InitializeComponent();
            this.Controls.Add(radDropDownList1 = new RadDropDownList());
            radDropDownList1.ItemDataBound += new ListItemDataBoundEventHandler(radDropDownList1_ItemDataBound);
            radDropDownList1.Dock = DockStyle.Top;
        }
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            radDropDownList1.DataSource = new TestsCollection();
     
            radDropDownList1.DisplayMember = "Name";
            radDropDownList1.ValueMember = "Id";
        }
     
        void radDropDownList1_ItemDataBound(object sender, ListItemDataBoundEventArgs args)
        {
            var test = args.NewItem.DataBoundItem as Test;
            if (test == null)
            {
                return;
            }
     
            args.NewItem.Image = test.Image;
        }
    }
     
    public class Test
    {
        public int Id
        {
            get;
            set;
        }
     
        public string Name
        {
            get;
            set;
        }
     
        public Image Image
        {
            get;
            set;
        }
     
        public Test(int id, string name, Image image)
        {
            this.Id = id;
            this.Name = name;
            this.Image = image;
        }
    }
     
    public class TestsCollection : BindingList<Test>
    {
        public TestsCollection()
        {
            this.Add(new Test(1, "Test1", Properties.Resources.ar));
            this.Add(new Test(2, "Test2", Properties.Resources.ca));
        }
    }

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
    Telerik WinForms MVP
Back to Top