Newbie: Simple Databinding in WinForms

Posted on 2007-07-19
Last Modified: 2013-12-17
Hi experts,

I have a small WinForms application that contains
- a DataGridView,
- a BindingNavigator,
- a BindingSource and
- a Textbox

The DataGridView shows commodities coming from a database and the BindingNavigator helps the user scrolling through the rows of commodities. DataGridView and BindingNavigator are connected to the same BindingSource. Everything works as expected.

However: I want the TextBox to show a certain value (i.e. the commodity's name) of the currently focused row in the DataGridView.

Thus I set a databinding in the Forms Load()-event like this:
            TextBox.DataBindings.Add("Text", commodityDataSource, "CommodityName");

When I start the application, the TextBox shows data from the first DataGridView row as expected.

But here is my problem: When I select another row in the DataGridView via Mouse or via Navigator the content of the TextBox is NOT updated.

What am I missing? Thanks for any help!
Question by:softwarea
    LVL 9

    Expert Comment

    You'll have to capture the event of hte selected row.
    LVL 9

    Expert Comment

    I believe you can trap it with the "RowEnter" event, then grab the CommodityName for the current row and populate the text box.
    LVL 9

    Expert Comment

    LVL 9

    Accepted Solution

    It'll be something similar to this:

    private void dataGridView1_RowEnter(object sender,
        DataGridViewCellEventArgs e)
        textbox1.text = dataGridView1.Rows[e.RowIndex].Cells[0].value;

    Author Comment

    Sorry, sorry, sorry! Thank you for your answer traxion, but I could not answer earlier.
    Ok, so I really have to take care of data scrolling manually.
    I thought/hoped that the databinding mechanism would do that automatically.
    So, thank you again!

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now