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

RadColorBox BindingSource

1 Answer 91 Views
Buttons, RadioButton, CheckBox, etc
This is a migrated thread and some comments may be shown as answers.
Mohamed
Top achievements
Rank 1
Mohamed asked on 31 May 2015, 04:43 PM
how can i use a binding source to bind values to radColorBox  while binding source is list of string of color names

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 01 Jun 2015, 02:09 PM
Hi Mohamed,

Thank you for writing.

You should create a Binding and use it Format and Parse events to convert from String to Color and vice versa. For example:
public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
    public RadForm1()
    {
        InitializeComponent();
    
        BindingList<ColorInfo> colorNames = new BindingList<ColorInfo>();
 
        colorNames.Add(new ColorInfo() { ColorName = "Red" });
        colorNames.Add(new ColorInfo() { ColorName = "Green" });
        colorNames.Add(new ColorInfo() { ColorName = "Blue" });
 
        BindingSource source = new BindingSource();
        source.DataSource = colorNames;
        radBindingNavigator1.BindingSource = source;    
        Binding binding = new Binding("Value", source, "ColorName", true);
 
        binding.Format += binding_Format;
        binding.Parse += binding_Parse;
        radColorBox1.DataBindings.Add(binding);
    }
 
    void binding_Format(object sender, ConvertEventArgs e)
    {
        string val = e.Value.ToString();
        switch (val)
        {
            case "Red":
                e.Value = Color.Red;
                break;
            case "Blue":
                e.Value = Color.Blue;
                break;
            case "Green":
                e.Value = Color.Green;
                break;
            default:
                e.Value = Color.FromArgb(int.Parse(e.Value.ToString()));
                break;
        }
    }
 
    void binding_Parse(object sender, ConvertEventArgs e)
    {
        e.Value = ((Color)e.Value).ToArgb().ToString();
    }
}
 
class ColorInfo
{
    public string ColorName { get; set; }
}

I hope this will be useful.

Regards,
Dimitar
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
Buttons, RadioButton, CheckBox, etc
Asked by
Mohamed
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or