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

bind image to a databind combobox

3 Answers 183 Views
ComboBox and ListBox (obsolete as of Q2 2010)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
anh
Top achievements
Rank 1
anh asked on 15 Nov 2010, 06:48 AM

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.

3 Answers, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 15 Nov 2010, 04:45 PM
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
0
anh
Top achievements
Rank 1
answered on 15 Nov 2010, 05:30 PM

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";

0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 16 Nov 2010, 07:52 AM
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
Tags
ComboBox and ListBox (obsolete as of Q2 2010)
Asked by
anh
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
anh
Top achievements
Rank 1
Share this question
or