Results 1 to 4 of 4

Thread: [RESOLVED] Binding textbox to datagridview

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Resolved [RESOLVED] Binding textbox to datagridview

    I have created a dataset and a bindingsource. I set the datagridview datasource to the bindingsource and do the same for the textbox. As I move through my datagridviews rows, the textbox follows along nicely. When I edit the textbox or datagrid, the change will only show up once I change rows. My problem is if I edit the textbox and click on my save button which does this:

    Code:
    this.Validate();
    this.persDataBindingSource.EndEdit();
    this.persDataTableAdapter.Update(this.dsDocs.PersData);
    It does not actually save the change because the dataset has not changed yet. How do I get this to work?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Binding textbox to datagridview

    I would have thought that that code would have pushed the new value to the data source before saving, but if it doesn't you can try calling the WriteValue method of the TextBox's Binding:
    Code:
    this.textBox1.Bindings["Text"].WriteValue();
    That will force the value in the control to be written to the data source. It should be noted that the data source is the BindingSource rather than the DataTable. You may have to call WriteValue and then EndEdit perhaps.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Binding textbox to datagridview

    Crap, I have 2 bindingsources on this form and I typed the wrong one, no wonder no update. All is good now. Thanks for the reply JM.

  4. #4
    Lively Member
    Join Date
    May 2008
    Location
    Manila, Philippines
    Posts
    81

    Re: Binding textbox to datagridview

    Quote Originally Posted by jmcilhinney
    I would have thought that that code would have pushed the new value to the data source before saving, but if it doesn't you can try calling the WriteValue method of the TextBox's Binding:
    Code:
    this.textBox1.Bindings["Text"].WriteValue();
    That will force the value in the control to be written to the data source. It should be noted that the data source is the BindingSource rather than the DataTable. You may have to call WriteValue and then EndEdit perhaps.
    hi, i have seen the WriteValue() method w/c says
    Reads the current value from the control property and writes it to the data source.

    i have controls in my forms which were binded on a datasource upon form load, when i try to change the value of my textbox in run time, the rowstate of the datasource does not change to added, do i need to trigger the WriteValue() method so that the datasource rowstate will change to added?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width