Runtime data binding by LINQ or anything similar

5 posts, 1 answers
  1. Toshiyuki Tega
    Toshiyuki Tega avatar
    13 posts
    Member since:
    Sep 2012

    Posted 15 Oct 2009 Link to this post

    I'm developing an application which makes use of artificial fields. Artificial fields are added by a user at runtime, so I'm creating and feeding a XML configuration dynamically. It is working well so far but I have a problem when it comes to data binding.

    On a windows form, I have a RadGridView, which is supposed to display all the rows in a table that is currently selected by a user, but I don't know how to bind the data to this grid correctly.

    According to a post in Ask for A sample about modifying reversemapping.config/app.config, I wrote a custom TypeDescriptionProvider, which is almost exactly the same as the one mentioned in the post.

    As this is the case, if I understand correctly, I cannot use ObjectProvider/ObjectView components on VS Form Designer for data-binding purpose. When I use ObjectProvider/ObjectView to bind data, the grid shows empty content, although the table isn't empty actually. When I strip TypeDescriptionProvider attribute from the persistent class, in turn, it works fine except the artificial fields, but it is not a solution at all because I want these artificial fields appear in the grid.

    So I'm trying to do data binding on the fly purely by code. OA Documentation suggests something like
    IObjectScope scope = ObjectScopeProvider.ObjectScope(); 
    var result = (from c in scope.Extent<MyDataType>() select c).ToList(); 
    radGridView.DataSource = result; 
     

    It actually shows all the data rows in the grid but, since it expands to a list, the grid complains when you try to edit and/or add rows.

    In summary, I need a data binding way that can handle artificial fields correctly. Also it should be created by code at runtime and allow users to modify the rows somehow.

    What would the best practice of data binding look like under this situation? Any help would be appreciated.
  2. Answer
    Zoran
    Admin
    Zoran avatar
    534 posts

    Posted 16 Oct 2009 Link to this post

    Hello Toshiyuki Tega,

    To enable Inserting/Editing operations from the RadGridView, there are few steps required as the use of the OpenAccess type descriptor has some specifics that the UI controls can not handle out of the box.

    • The first thing is the way in which you are binding the RadGridView. To be able to do all CRUD operations the grid should be bound to a BindingList<T> instead of a List<T>.

    Here is a small update on the code you had found in our documentation:
    IObjectScope scope = ObjectScopeProvider.ObjectScope();
    var result = (from c in scope.Extent<MyDataType>() select c).ToList();
    BindingList<MyDataType> bl = new BindigList<MyDataType>(result);
    radGridView.DataSource = bl;

    • Next step is to attach to the DefaultValuesNeeded event of the RadGridView. This event is fired when you try to insert some new record. There you should add the new record to your scope prior to setting any values. This is the code that accomplishes this:
    private void radGridView1_DefaultValuesNeeded(object sender, GridViewRowEventArgs e)
    {
        Person p = (Person)e.Row.DataBoundItem;
        if (!scope.Transaction.IsActive)
        {
            scope.Transaction.Begin();
        }
            scope.Add(p);
    }

    • The last step you should follow is the handling of the RadGridView.DataBindings.CollectionChanged event. There you should just commit the transaction so you can add the new record in the database.
    void DataBindings_CollectionChanged(object sender, CollectionChangeEventArgs e)
    {
        if (scope.Transaction.IsActive)
        {
            scope.Transaction.Commit();
        }
    }
    I hope you can adjust this code for the requirements of your project. Here I have shown how to basically be able to use the adding of new records functionality of the RadGridView, but you can do similar actions to enable editing as well.

    If any additional questions arise, please send us your feedback.

    Kind regards,
    Zoran
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. DevCraft banner
  4. Toshiyuki Tega
    Toshiyuki Tega avatar
    13 posts
    Member since:
    Sep 2012

    Posted 17 Oct 2009 Link to this post

    Zoran,

    Thank you very much for your prompt support. It is working well in my simple scenario so far.
  5. Michael Winter
    Michael Winter avatar
    7 posts
    Member since:
    Jun 2009

    Posted 23 Oct 2009 Link to this post

    Hi,
    I have the same problem, but use a datagridview control.

    How do I have to modify the example?

    Thanks
    Michael
  6. Zoran
    Admin
    Zoran avatar
    534 posts

    Posted 28 Oct 2009 Link to this post

    Hello Michael Winter,

    The DataGridView has the DevaultValuesNeeded and DataBindings.CollectionChanged events as the Telerik RadGridView for windows forms has. This means that you should be able to handle the issue in the same fashion as suggested in my previous post.

    Kind regards,
    Zoran
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top
DevCraft banner