0

Using Entity data model in a Windows Forms Project, I want to bind simultaneously Orders entity to datagridview and to textBox, and textBox has to display OrderID value depending on the current line in the datagrid.

The code I used in Form load event is next:

    using(NorthwindEntities context = new NorthwindEntities())
    {
    ordersDataGridView.DataSource = context.Orders;
    OrderNumberTextBox.   ...
    }

For this case, what is the right syntax to bind Textbox ? Thank you.

Sami-L
  • 5,553
  • 18
  • 59
  • 87
  • textBox haven't datasource so you should to bind it by your hand – Likurg Apr 27 '12 at 09:22
  • I did not said textBox have datasource property, my question is how to make it show the field value? This text box had been dragged and dropped from datasource window to the form. – Sami-L Apr 27 '12 at 11:03

1 Answers1

5

Bind a BindingSource object to your context.Orders, bind your DataGridView to the BindingSource, and then through the TextBox.DataBindings property, bind to appropriate property of your TextBox to your BindingSource. The BindingSource object will manage the currency state so that the TextBox will change when you select different items in your DataGridView.

The binding will look similar to something to this:

OrderNumberTextBox.DataBindings("Text", bindingSource, "OrderID");
Brad Rem
  • 6,036
  • 2
  • 25
  • 50