ComponentOne LiveLinq > Getting Started with LiveLinq > Hierarchical LiveLinq Binding > LiveLinq implementation in WinForms |
The previous section described how to migrate a traditional data-bound application to use LiveLinq. The resulting application used BindingSource objects and code that was generated by Visual Studio at design time.
If we wanted to create a new LiveLinq application from scratch, we could make it even simpler.
Here are the steps:
C# |
Copy Code
|
---|---|
using C1.LiveLinq; using C1.LiveLinq.AdoNet; using C1.LiveLinq.LiveViews; private void Form1_Load(object sender, EventArgs e) { // get the data var ds = GetData(); // create a live view with Categories and Products: var liveView = from c in ds.Categories.AsLive() join p in ds.Products.AsLive() on c.CategoryID equals p.CategoryID into g select new { c.CategoryID, c.CategoryName, Products = g }; // bind view to controls DataBind(liveView); } |
The code is straightforward. It calls a GetData method to load the data into a DataSet, creates a LiveLinq view using a LINQ statement similar to the one we used earlier, and then calls DataBind to bind the controls on the form to the view.
Here is the implementation of the GetData method:
C# |
Copy Code
|
---|---|
NORTHWNDDataSet GetData() { NORTHWNDDataSet ds = new NORTHWNDDataSet(); new NORTHWNDDataSetTableAdapters.ProductsTableAdapter() .Fill(ds.Products); new NORTHWNDDataSetTableAdapters.CategoriesTableAdapter() .Fill(ds.Categories); return ds; } |
GetData creates a NORTHWNDDataSet, fills the “Products” and “Categories” tables using the adapters created by Visual Studio at design time, and returns the data set.
Here is the implementation of the DataBind method:
C# |
Copy Code
|
---|---|
void DataBind(object dataSource) { // bind ComboBox comboBox1.DataSource = dataSource; comboBox1.DisplayMember = "CategoryName"; comboBox1.ValueMember = "CategoryID"; // bind DataGridView dataGridView1.DataMember = "Products"; dataGridView1.DataSource = dataSource; // bind TextBox controls BindTextBox(textBox1, dataSource, "ProductName"); BindTextBox(textBox2, dataSource, "UnitPrice"); BindTextBox(textBox3, dataSource, "QuantityPerUnit"); BindTextBox(textBox4, dataSource, "UnitsInStock"); } void BindTextBox(TextBox txt, object dataSource, string dataMember) { var b = new Binding("Text", dataSource, "Products." + dataMember); txt.DataBindings.Add(b); } |
DataBind sets the data binding properties on each control to bind them to our LiveLinq view. This is exactly the same thing we did before using the property window editors, except this time we are doing it all in code and binding the controls directly to the LiveLinq view instead of going through a BindingSource component.
If you run the project now, you will see that it still works exactly as before.
Writing the data binding code usually takes a little longer than using the design time editors and letting Visual Studio write the code for you. On the other hand, the result is usually code that is simpler, easier to maintain, and easier to port to other platforms (as we will do in the next section).
Either way, you can use LiveLinq views as binding sources for the controls on the form.