Is there a way to Convert binding dataMember?
-
trying to convert dataMember from int to Color
color1.DataBindings.Add("BackColor",NavBill.DataSource,"Color1" );
Control : color1 is panel Binding dataMember : "Color1" is columns(int,null) How can convert dataMemer value from (int) to (Color) before assignment to property?
-
trying to convert dataMember from int to Color
color1.DataBindings.Add("BackColor",NavBill.DataSource,"Color1" );
Control : color1 is panel Binding dataMember : "Color1" is columns(int,null) How can convert dataMemer value from (int) to (Color) before assignment to property?
Yes you just need to use a Format event handler on the Binding object For example
private void AddBinding() { // Enable formatting and add the event handler Binding b = color1.DataBindings.Add("BackColor", NavBill.DataSource, "Color1", true); b.Format += PanelBackColorBinding\_Format; } private void PanelBackColorBinding\_Format(Object sender, ConvertEventArgs e) { if (e.DesiredType == typeof(Color)) { // Get the integer value from the DataColumn Int32 number = (Int32)e.Value; // Get the Color corresponding to the number // \*\*\* PUT YOUR OWN CODE HERE \*\*\* Color col = GetColor(number); // Assign the correct Color back into e.Value e.Value = col; } }
Make sure that a default color is assigned to e.Value if there is any chance that the column will have missing or non convertable values.