如何将控件的BackColor(或其他属性)绑定到存储在DataSource中的字符串值



所以我有一个数据库,其中的颜色值以ARBG格式存储为INTEGERS。我想做的是将控件的背景颜色绑定到数据库,以便颜色根据所选记录与控件背景相匹配。(即,每条记录都有一种颜色,当表单更改绑定源上的记录显示时,控件背景将更改,以匹配该记录数据库中的颜色INT(。

到目前为止,我有这个(不起作用(:

pictureBox1.DataBindings.Add("BackColor", BindingSource1, "ColorINT");

问题是数据库将颜色存储为INTEGER,数据绑定需要一种颜色,但我无法调用color.FromArgb函数将绑定改回颜色。我该怎么做?

感谢吉米的回复,我终于明白了。为可能有类似问题的人发帖:

private void IntegerToColor(object sender, ConvertEventArgs cevent)
{
if (cevent.DesiredType != typeof(Color)) return;
cevent.Value = Color.FromArgb(Convert.ToInt32(cevent.Value));
}

//然后加载

Binding PicColorBind = new Binding("BackColor", BindingSource1, "ColorINT");
PicColorBind.Format += new ConvertEventHandler(IntegerToColor);
picColor.DataBindings.Add(PicColorBind);

最新更新