New to Telerik UI for WinFormsStart a free 30-day trial

Data Binding

Updated over 6 months ago

To bind data to RadCarousel:

  • Set the DataSource property.

  • Handle the NewCarouselItemCreating event. In the event handler, create a RadItem descendant instance.

  • Handle the ItemDataBound event. In this event you have access to both the data item and to the RadItem instance.

This same pattern holds true, regardless of the type of data being bound to.

Example

The example below creates a generic list of an object called "Feature". "Feature" has "ID" and "Name" properties. In the NewCarouselItemCreating event handler, RadButtonElement instances are created. In the ItemDataBound event handler, the button text is assigned the "Name" property of the "Feature" object. The "ID" property of the "Feature" object is stored in the RadButtonElement Tag property for later use in the Click event handler.

WinForms RadCarousel Data Binding Example

The Features Object

C#
public class Features
{
    public Features(int id, string name)
    {
        _id = id;
        _name = name;
    }
    private int _id;
    public int ID
    {
        get
        {
            return _id;
        }
        set
        {
            _id = value;
        }
    }
    private string _name;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
        }
    }
}

Binding RadCarousel to Generic List

C#
private void CarouselDataBinding_Load(object sender, EventArgs e)
{
    // Describe the carousel path
    CarouselBezierPath path = new CarouselBezierPath();
    path.CtrlPoint1 = new Telerik.WinControls.UI.Point3D(86, 76, 70);
    path.CtrlPoint2 = new Telerik.WinControls.UI.Point3D(10, 20, 0);
    path.FirstPoint = new Telerik.WinControls.UI.Point3D(14, 77, 70);
    path.LastPoint = new Telerik.WinControls.UI.Point3D(90, 20, 0);
    path.ZScale = 500;
    this.radCarousel1.CarouselPath = path;
    radCarousel1.ItemDataBound += new ItemDataBoundEventHandler(radCarousel1_ItemDataBound);
    radCarousel1.NewCarouselItemCreating += new NewCarouselItemCreatingEventHandler(radCarousel1_NewCarouselItemCreating);
    this.Click += new EventHandler(CarouselDataBinding_Click);
    // Create a generic list of Feature objects and bind it
    List<Features> products = new List<Features>();
    products.Add(new Features(1, "Products"));
    products.Add(new Features(2, "Services"));
    products.Add(new Features(3, "Consulting"));
    radCarousel1.DataSource = products;
}
private void radCarousel1_ItemDataBound(object sender, ItemDataBoundEventArgs e)
{
    if (e.DataBoundItem is RadButtonElement)
    {
        // get the RadButtonElement created in the NewCarouselItemCreating event.
        // The RadItem is represented by the e.DataBoundItem property.
        // Assign properties based on bound data - represented by e.DataItem
        RadButtonElement button = (e.DataBoundItem as RadButtonElement);
        button.Text = (e.DataItem as Features).Name;
        button.Tag = (e.DataItem as Features).ID;
        button.Font = new Font("Arial", 20);
        button.Click += new EventHandler(CarouselDataBinding_Click);
    }
}
private void radCarousel1_NewCarouselItemCreating(object sender, NewCarouselItemCreatingEventArgs e)
{
    e.NewCarouselItem = new RadButtonElement();
}
void CarouselDataBinding_Click(object sender, EventArgs e)
{
    MessageBox.Show("You clicked ID " + (sender as RadButtonElement).Tag.ToString());
}

See Also

In this article
ExampleSee Also
Not finding the help you need?
Contact Support