Entity Framework How to bind textbox to model in Windows Forms

c# entity-framework linq-to-entities linq-to-objects winforms

Question

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.

1
0
4/9/2016 12:13:36 AM

Accepted Answer

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");
5
4/27/2012 11:19:56 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow