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:

1.   Create a new WinForms application

2.   Use the Data | Add New DataSource menu and add a reference to the NORTHWND.MDF database. Accept all the default options offered by the wizard, and pick all the tables in the database.

3.   Add the controls to the form as before: one ComboBox, one DataGridView, four TextBox controls, and a few Label controls.

4.   Add a reference to the C1.LiveLinq.dll assembly to the project.

5.   Double-click the form add the following 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:

 

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:

 

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 = newBinding("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.


Send us comments about this topic.
Copyright © GrapeCity, inc. All rights reserved.