Sami-L
Sami-L

Reputation: 5705

Entity Framework How to bind textbox to model in Windows Forms

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.

Upvotes: 0

Views: 9111

Answers (1)

Brad Rem
Brad Rem

Reputation: 6026

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");

Upvotes: 5

Related Questions