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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Databinding not working...?

Databinding not working...?

Scheduled Pinned Locked Moved C#
question
5 Posts 2 Posters 0 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.
  • M Offline
    M Offline
    mikker_123
    wrote on last edited by
    #1

    The thing I want to do is simple: bind Control to String... so I must be missing something obvious... here's code: [code] string stringDataSource = "Hello"; private void Form1_Load(object sender, EventArgs e) { textBox1.DataBindings.Add("Text", stringDataSource, null); } private void btnWork_Click(object sender, EventArgs e) { MessageBox.Show(stringDataSource); } [/code] Tnx

    J 1 Reply Last reply
    0
    • M mikker_123

      The thing I want to do is simple: bind Control to String... so I must be missing something obvious... here's code: [code] string stringDataSource = "Hello"; private void Form1_Load(object sender, EventArgs e) { textBox1.DataBindings.Add("Text", stringDataSource, null); } private void btnWork_Click(object sender, EventArgs e) { MessageBox.Show(stringDataSource); } [/code] Tnx

      J Offline
      J Offline
      Josh Smith
      wrote on last edited by
      #2

      Since strings are immutable in .NET, this shouldn't work. A string is never changed. When a string reference points to a new value, it is literally pointing to a new string in the string pool (i.e. it points to a different memory address). Plus, databinding requires that you bind to a property, not to a value directly. Expose your string as a property on another object and then bind to that object & property. Josh

      M 1 Reply Last reply
      0
      • J Josh Smith

        Since strings are immutable in .NET, this shouldn't work. A string is never changed. When a string reference points to a new value, it is literally pointing to a new string in the string pool (i.e. it points to a different memory address). Plus, databinding requires that you bind to a property, not to a value directly. Expose your string as a property on another object and then bind to that object & property. Josh

        M Offline
        M Offline
        mikker_123
        wrote on last edited by
        #3

        First of all, tnx a lot Josh... I surely better understand whole thing now. On another matter -> is it possible to bind to a member of Collection? If I'm having this situation: public class ModificatorData { private List address; public List Address { get { return address; } set { address = value; } } } Is it possible that I say -> textBox1 bind to first member of Address collection... textBox2 to second member... and so on? I guess answer is no because it's what DataGrids and DataSets do and you can't easily expose every member as Property, but I'm still courious... Again -> tnx for your help

        J 1 Reply Last reply
        0
        • M mikker_123

          First of all, tnx a lot Josh... I surely better understand whole thing now. On another matter -> is it possible to bind to a member of Collection? If I'm having this situation: public class ModificatorData { private List address; public List Address { get { return address; } set { address = value; } } } Is it possible that I say -> textBox1 bind to first member of Address collection... textBox2 to second member... and so on? I guess answer is no because it's what DataGrids and DataSets do and you can't easily expose every member as Property, but I'm still courious... Again -> tnx for your help

          J Offline
          J Offline
          Josh Smith
          wrote on last edited by
          #4

          Technically, you could have each textbox bind to a different element in a collection, but that isn't the intended use of simple databinding. Simple databinding (binding a control property to a data source property) is meant to allow multiple controls on a Form to display the various values found in one object (i.e. one Address object in a collection). The "current" object being bound to in a collection is scoped to a BindingContext. Each control using the same binding context (which is the default set up) will display a value from the current item. If you really want each textbox to display a value from a different address, you'd need to establish the binding for each control in a separate binding context. Then move the currency manager's current object in each binding context to set the "current" object for that control. Sound like fun? :wtf: Josh

          M 1 Reply Last reply
          0
          • J Josh Smith

            Technically, you could have each textbox bind to a different element in a collection, but that isn't the intended use of simple databinding. Simple databinding (binding a control property to a data source property) is meant to allow multiple controls on a Form to display the various values found in one object (i.e. one Address object in a collection). The "current" object being bound to in a collection is scoped to a BindingContext. Each control using the same binding context (which is the default set up) will display a value from the current item. If you really want each textbox to display a value from a different address, you'd need to establish the binding for each control in a separate binding context. Then move the currency manager's current object in each binding context to set the "current" object for that control. Sound like fun? :wtf: Josh

            M Offline
            M Offline
            mikker_123
            wrote on last edited by
            #5

            DataSet + DataGrid is much easier ;) Tnx a lot for your help.

            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