• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 967
  • Last Modified:

Newbie: Simple Databinding in WinForms

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!
  • 4
1 Solution
You'll have to capture the event of hte selected row.
I believe you can trap it with the "RowEnter" event, then grab the CommodityName for the current row and populate the text box.
It'll be something similar to this:

private void dataGridView1_RowEnter(object sender,
    DataGridViewCellEventArgs e)
    textbox1.text = dataGridView1.Rows[e.RowIndex].Cells[0].value;
softwareaAuthor Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now