ComponentOne Data Source for Entity Framework
Design-Time Features / Customizing View
In This Topic
    Customizing View
    In This Topic

    In many situations, you may want to use custom views that do not correspond directly to the tables and views provided by the database. LINQ is the perfect tool for these situations, allowing you to transform raw data using your language of choice using query statements that are flexible, concise, and efficient. C1DataSource makes LINQ even more powerful by making LINQ query statements live. That’s why its LINQ implementation is called LiveLinq. We will show the power of LiveLinq in Live Views. For now, suffice it to say that you can transform your view to shape it whatever way you want using LINQ operators without losing full updatability and bindability.

    Let’s try, for example, one of the LINQ operators, Select (also called projection), to customize the fields (properties) of our view.

    To customize a view, follow these steps:

    1. Using the project we created to demonstrate Paging, add a new form with a C1DataSource component using the same ContextType as before. Note that you can make this the startup form to save time when you run the project.
    2. Create a ViewSource in the ViewSourceCollection editor. Use the Products table in our sample database.
    3. Add a DataGrid to the form, but unlike previous examples, we won't actually bind its C1DataSource at design time. We’ll do this at run time in code because we want to implement a custom view.
    4. Add the following directive statements to the form's code:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Imports C1.LiveLinq.LiveViews

      To write code in C#

      C#
      Copy Code
      using C1.LiveLinq.LiveViews;
    5. In the form’s Load event, add the following code to create a custom live view and bind it to the grid:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      dataGridView1.DataSource = _
           (From p In C1DataSource1("Products").AsLive(Of Product)()
                                   Select New With
                                   {
                                       p.ProductID,
                                       p.ProductName,
                                       p.CategoryID,
                                       p.Category.CategoryName,
                                       p.SupplierID,
                                       .Supplier = p.Supplier.CompanyName,
                                       p.UnitPrice,
                                       p.QuantityPerUnit,
                                       p.UnitsInStock,
                                       p.UnitsOnOrder
                                   }).AsDynamic()

      To write code in C#

      C#
      Copy Code
      dataGridView1.DataSource =
           (from p in c1DataSource1["Products"].AsLive<Product>()
               select new
                    {
                        p.ProductID,
                        p.ProductName,
                        p.CategoryID,
                        CategoryName = p.Category.CategoryName,
                        p.SupplierID,
                        Supplier = p.Supplier.CompanyName,
                        p.UnitPrice,
                        p.QuantityPerUnit,
                        p.UnitsInStock,
                        p.UnitsOnOrder
                    }).AsDynamic();

      Here c1DataSource1["Products"] is a ClientCollectionView object. It is the view that is created by the view source that we set up in the designer. Note that if you need to access the view source itself in code, it is also available, as c1DataSource.ViewSources["Products"]. The AsLive() extension method call is needed to specify the item type of the view (Product) so LiveLinq operators can be applied to it. The result, c1DataSource1["Products"].AsLive<Product>(), is a View<Product>. The C1.LiveLinq.LiveViews.View is the main class of LiveLinq used for client-side live views. LINQ operators applied to live views preserve their updatability and bindability. They are the same usual LINQ operators, but the fact that they are applied to a live view gives them these additional features that are critically important for data binding applications.

      Note: AsDynamic() must be applied to this view because its result selector (the select new… code) uses an anonymous class. This is a minor LiveLinq limitation, only for anonymous classes. If you forget to add AsDynamic() to such view, you will be reminded by an exception.
    6. Save, build and run the application. The grid now shows the columns we defined in the ‘select’ clause of our LiveLinq view. Note also that all columns are modifiable. It may not seem like a big deal; however, it is an important feature that would be difficult to implement on your own for this customized view, especially when adding and deleting rows, which, as you can see here, is also supported. This is what we were referring to by stating that LiveLinq operators preserve updatability.

    Bindability is achieved because the views are always ‘live’; they aren’t simple snapshots of static data. To prove the point, construct an exact replica of the form that we’ve just built. At this stage, you might find it easier to add a menu to your application to make accessing the forms you’ve created easier. Save, build and run the application.  This time, open two instances of the form you just created and change some data in the grid on one of the forms. Notice how the corresponding data in the grid on the other form is changed automatically. Remember, you have not had to write a single line of code for the grids in these forms to synchronize the changes that you’ve just made.

    You will see more of what LiveLinq can do in the Live Views topic.