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. Data Binding a TextBox to a DataSet

Data Binding a TextBox to a DataSet

Scheduled Pinned Locked Moved C#
wpfwcfquestion
6 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.
  • W Offline
    W Offline
    woaksie
    wrote on last edited by
    #1

    I created a windows form. I added a SQLConnection, a SQLDataAdaptor, a DataSet all via the designer. I then added a TextBox and a ListBox and databound them both to a column in the DataSet. I then added a button that will show the next record with the following code; this.BindingContext[this.dsPeople1.People].Position ++; When I run this the ListBox’s selected line moves to the next record but the TextBox stays on the first record. Am I missing something? This is the code that has been generated for the TextBox and the ListBox. this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dsPeople1, "People.First_Name")); this.listBox1.DataSource = this.dsPeople1.People; this.listBox1.DisplayMember = "First_Name"; John

    H 1 Reply Last reply
    0
    • W woaksie

      I created a windows form. I added a SQLConnection, a SQLDataAdaptor, a DataSet all via the designer. I then added a TextBox and a ListBox and databound them both to a column in the DataSet. I then added a button that will show the next record with the following code; this.BindingContext[this.dsPeople1.People].Position ++; When I run this the ListBox’s selected line moves to the next record but the TextBox stays on the first record. Am I missing something? This is the code that has been generated for the TextBox and the ListBox. this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dsPeople1, "People.First_Name")); this.listBox1.DataSource = this.dsPeople1.People; this.listBox1.DisplayMember = "First_Name"; John

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      It all depends on how you bind. Notice that the textBox1's DataBindings binds to this.dsPeople1, not this.dsPeople1.People like the listBox1. Your binding expression, this.BindingContext[this.dsPeople1.People] also binds against People. These are very different. A BindingContext must use the exact same binding. this.dsPeople1 and this.dsPeople1.People are different contexts. So, change the binding for textBox1 to this:

      this.text1.DataBindings.Add(
      new Binding("Text", this.dsPeople1.People, "First_Name"));

      Microsoft MVP, Visual C# My Articles

      W 2 Replies Last reply
      0
      • H Heath Stewart

        It all depends on how you bind. Notice that the textBox1's DataBindings binds to this.dsPeople1, not this.dsPeople1.People like the listBox1. Your binding expression, this.BindingContext[this.dsPeople1.People] also binds against People. These are very different. A BindingContext must use the exact same binding. this.dsPeople1 and this.dsPeople1.People are different contexts. So, change the binding for textBox1 to this:

        this.text1.DataBindings.Add(
        new Binding("Text", this.dsPeople1.People, "First_Name"));

        Microsoft MVP, Visual C# My Articles

        W Offline
        W Offline
        woaksie
        wrote on last edited by
        #3

        Thanks for the reply. I tried that and I get the error; "Cannot create a child list for field People." This code is generated automatically when you set up the data binding for the textbox in the designer so should be okay, right??? John.

        H 1 Reply Last reply
        0
        • W woaksie

          Thanks for the reply. I tried that and I get the error; "Cannot create a child list for field People." This code is generated automatically when you set up the data binding for the textbox in the designer so should be okay, right??? John.

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          You got that error where? If you actually read the documentation for the Control.BindingContext, it clearly states what I said - the binding context must be the same as what was used to bind the control. For your TextBox in question, they are not the same. You could also try changing the binding for your ListBox to what the TextBox uses and changing your BindingContext accordingly as well.

          Microsoft MVP, Visual C# My Articles

          W 1 Reply Last reply
          0
          • H Heath Stewart

            You got that error where? If you actually read the documentation for the Control.BindingContext, it clearly states what I said - the binding context must be the same as what was used to bind the control. For your TextBox in question, they are not the same. You could also try changing the binding for your ListBox to what the TextBox uses and changing your BindingContext accordingly as well.

            Microsoft MVP, Visual C# My Articles

            W Offline
            W Offline
            woaksie
            wrote on last edited by
            #5

            The full error is: An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll Additional information: Cannot create a child list for field People. I single stepped through the program and the error happens after this line of code runs (every line of code in the Form1 constructor steps through okay but it fails when the last line has been executed and it returns to this line. If I break into it is breaks on the line after this one - int i = 0). Application.Run(new Form1()); I read the documentation. Can you get it to work using the designer interface? Or even via code? John.

            1 Reply Last reply
            0
            • H Heath Stewart

              It all depends on how you bind. Notice that the textBox1's DataBindings binds to this.dsPeople1, not this.dsPeople1.People like the listBox1. Your binding expression, this.BindingContext[this.dsPeople1.People] also binds against People. These are very different. A BindingContext must use the exact same binding. this.dsPeople1 and this.dsPeople1.People are different contexts. So, change the binding for textBox1 to this:

              this.text1.DataBindings.Add(
              new Binding("Text", this.dsPeople1.People, "First_Name"));

              Microsoft MVP, Visual C# My Articles

              W Offline
              W Offline
              woaksie
              wrote on last edited by
              #6

              You are right! When I tried your solution the first time I changed the second parameter but not the third. When I made both changes it worked. So it looks like a bug in the designer. When I set the DataBindings in the property window of the TextBox it generates code the incorrect way. So now I am not binding that way but via my own code. Thanks for your help. John.

              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