Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Is there a way to Convert binding dataMember?

Is there a way to Convert binding dataMember?

Scheduled Pinned Locked Moved C#
wpfwcfquestion
2 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Geanny Martin
    wrote on last edited by
    #1

    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?

    A 1 Reply Last reply
    0
    • G Geanny Martin

      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?

      A Offline
      A Offline
      Alan N
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups