locked
Textbox goes blank after exiting - Linq2SQL and databinding to textbox RRS feed

  • Question

  • I have a nagging problem that should be something simple but it's not working for me. I have a Linq2SQL result that I am binding to a datagridview (only one column) (I did try binding to a BindingSource too) and also to textboxes to edit the fields...

    I have several tables / fields / textboxes that exhibit the problem so it must be a core problem in my thinking...

    The problem is with numeric fields and date fields - when I try to edit the textbox, then exit, the text in the text box reverts back to what it was (blank or previous data) but my text fields work fine...

    here is an example of my databindings lines:

    _incomeNameTextBox.DataBindings.Clear();
    _incomeNameTextBox.DataBindings.Add("Text", _incomesDataGridView.DataSource, "IncomeName");

    _incomeAmountTextBox.DataBindings.Clear();
    _incomeAmountTextBox.DataBindings.Add("Text", _incomesDataGridView.DataSource, "IncomeAmount");

    IncomeName edits just fine while IncomeAmount does not - and like I said I have a few tables / textboxes that do this... What am I doing wrong?

     

     

    • Moved by Larcolais Gong Friday, June 10, 2011 1:12 PM (From:LINQ Project General)
    Tuesday, June 7, 2011 10:38 PM

Answers

  • OK Third post in a row :)

    I figured out the Checkbox thing - basically it was because I was allowing nulls in my database and it could not handle that - so I just made a default false for that column in my tables

     

    • Proposed as answer by Neddy Ren Tuesday, June 14, 2011 3:35 AM
    • Marked as answer by JamieC7 Tuesday, June 14, 2011 10:46 AM
    Monday, June 13, 2011 5:33 PM

All replies

  • ok - how about this... Does anyone have a link to a good tutorial / book / video for creating Winform Database Front End Apps? master - Detail with displaying records in a grid and editing data via textboxes? the idea's not that crazy is it? LOL

    I want to distribute my database with my app, so either SQL Excpress or CE - I also like Linq2SQL a lot (although in all this research I find that you cannot use BindingSource.Find ugh)

    I've looked at the videos on Winforms Learning and the ONE that I want has missing video in the first and the second picks up after obvious time has passed so there's a lot of misses info

    any ideas are appreciated


    Check out my CNC (and more) projects at http://www.backyard-workshop.com
    Wednesday, June 8, 2011 7:17 PM
  • Hello,

    Thank you for posting.

    From your post, it looks that your question is more than Winform Data Binding one. I suggest you can ask your question into Winform forum. http://social.msdn.microsoft.com/Forums/en-US/winforms/threads

    If you have any concern, please feel free to let me know.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 10, 2011 1:11 PM
  • Thanks for moving it - hopefully someone will have an idea to try as I'm not getting anywhere with a solution yet :)

     


    Check out my CNC (and more) projects at http://www.backyard-workshop.com
    Friday, June 10, 2011 10:03 PM
  • Hi JamieC7,

    Welcome to the MSDN Forum.

    It seems that he code you've provided is right. But the DataBinding will not help you to save your changed data. You will need to submit your data to the DataBase.

    Just as you use Databinding and use DataAdapter to fill the data to your datagridview, you must use updatecommand.

    To the questions in your replies, if you want to search something form your bindingsource, you can use BindingSource.filter ="Your expressions".
    Have a look at the following demon project in my skydrive:
    http://cid-0331372fa6a33be3.office.live.com/self.aspx/.Public/WFA^_BindingSourceFilter.rar

    And you can also use master/detailed dataset with DataGridView:

    Walkthrough: Creating a Master/Detail Form Using Two Windows Forms DataGridView Controls:
    http://msdn.microsoft.com/en-us/library/y8c0cxey.aspx

    If you have any questions, please feel free to let me know.

    Best Regards


    Neddy Ren [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Monday, June 13, 2011 8:53 AM
  • Thanks for the suggestions Neddy - I will check both of those out

    but my problem is that as soon as I exit the textbox the value in the textbox reverts back to what it was - even if there's already a value (say 120.00) when I put 150.00 as soon as I exit the textbox - either by clicking on another textbox or hitting tab or even trying to hit the save button... basically as soon as the current textbox I'm editing looses focus then it reverst back to 120

    I don't have any event handlers wired up for any of the textboxes - only some buttons

    it's very strange (and frustrating)

    Jamie


    Check out my CNC (and more) projects at http://www.backyard-workshop.com
    Monday, June 13, 2011 11:00 AM
  • Ok So this is my accout at work - so sorry for the double post :)

    I looked at the example and reworked my code to use the SQLDataAdapter instead of the Linq2SQL and it works except for one small thing... I hae a checkbox column (bit) that if I databind then I hae problems (like I can traerse the rows in the datagrid but the textboxes don't change to the row - they stay on the first) - another weird one... if I comment out the databind on the checkbox then eerything works perfect...

    but that also brings up another question... shouldn't I be able to bind to the Linq2SQL query resutls? it displayed the data just fine, just couldn't edit - note I was trying to bind the bindingSource right to the Linq2SQL query results...

    Jamie 

    Monday, June 13, 2011 12:38 PM
  • OK Third post in a row :)

    I figured out the Checkbox thing - basically it was because I was allowing nulls in my database and it could not handle that - so I just made a default false for that column in my tables

     

    • Proposed as answer by Neddy Ren Tuesday, June 14, 2011 3:35 AM
    • Marked as answer by JamieC7 Tuesday, June 14, 2011 10:46 AM
    Monday, June 13, 2011 5:33 PM
  • Hi Jamie,

    Thanks for sharing your solutions.

    On the other hand, such issue would be handled more efficiently if you've asked the questions in the Linq forum since the question is Linq related.

    If you have any questions, please feel free to tell us.

    Best Regards


    Neddy Ren [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 14, 2011 3:35 AM
  • LOL Thanks Neddy - but you guys moved my post FROM the Linq forum LOL so blame Larcolais Gong hehe

    Check out my CNC (and more) projects at http://www.backyard-workshop.com
    Tuesday, June 14, 2011 10:46 AM