This is a migrated thread and some comments may be shown as answers.

Simple WinForms example ?

3 Answers 148 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Stefan
Top achievements
Rank 1
Stefan asked on 16 Aug 2010, 04:49 PM
I am struggling with OA to get a simple WinForms application running.

What I have done is to add a new domain model and add some tables from my db and build the solution.
In the data sources tab I have created new data sources on basis of the objects created by OA and
dragged them on the form where they have created the standard controls.

When I run the application, all controls remain empty. What am I missing ?

Is the OA approach not that straightforward ?

Sadly I did not find any simple Winforms example to learn from.

Thanks in advance,
Stefan

3 Answers, 1 is accepted

Sort by
0
Damyan Bogoev
Telerik team
answered on 17 Aug 2010, 05:13 PM
Hi Stefan,

The controls do not show any data because their binding source controls are bound in the following way:

this.bindingSourceName.DataSource = typeof(Namespace.ClassName);

You should bind the BindingSource controls using the following approach in order to load the necessary data:

private YourCotnextName context;
 
public MainApp()
{
    this.InitializeComponent();
    this.context = new YourCotnextName();
 
    this.bindingSourceName.DataSource = this.context.DataEndPoint.ToList();
 
    this.FormClosing += new FormClosingEventHandler(MainApp_FormClosing);
}
 
private void MainApp_FormClosing(object sender, FormClosingEventArgs e)
{
    this.context.Dispose();
}

You could find this getting started tutorial useful, it demonstrates how to integrate a WinForms application with Telerik OpenAccess ORM, as well as this code library example.
Hope that helps.


Regards,
Damyan Bogoev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Craig Parks
Top achievements
Rank 1
answered on 25 Aug 2010, 07:02 PM
Wondering if you have a simple winforms example like this that demonstrates how to write changes in radgridview back to a bindingsource that uses the domain ORM?

I have used something like this but new records aren't written back to the db:

private

 

void btnSave_1_Click(object sender, EventArgs e)

 

{


vodGroupTypeBindingSource.EndEdit();
vodMediaTypeBindingSource.EndEdit();
vodMediumBindingSource.EndEdit();
vodGroupBindingSource.EndEdit();            
vodMediaAccessBindingSource.EndEdit();
vodGroupCustTypeBindingSource.EndEdit();
vodGroupMediaItemBindingSource.EndEdit();
  
dbContext.SaveChanges();
  
dbContext.Refresh(RefreshMode.OverwriteChangesFromStore, dbContext.VodGroupCustTypes);
dbContext.Refresh(RefreshMode.OverwriteChangesFromStore, dbContext.VodGroupMediaItems);
dbContext.Refresh(RefreshMode.OverwriteChangesFromStore, dbContext.VodGroups);
dbContext.Refresh(RefreshMode.OverwriteChangesFromStore, dbContext.VodGroupTypes);
dbContext.Refresh(RefreshMode.OverwriteChangesFromStore, dbContext.VodMedia);
dbContext.Refresh(RefreshMode.OverwriteChangesFromStore, dbContext.VodMediaAccesses);
dbContext.Refresh(RefreshMode.OverwriteChangesFromStore, dbContext.VodMediaTypes);
  
vodMediaTypeBindingSource.DataSource = dbContext.VodMediaTypes.ToList();
vodGroupTypeBindingSource.DataSource = dbContext.VodGroupTypes.ToList();
vodMediumBindingSource.DataSource = dbContext.VodMedia.ToList();
vodGroupBindingSource.DataSource = dbContext.VodGroups.ToList();
vodMediaAccessBindingSource.DataSource = dbContext.VodMediaAccesses.ToList();
vodGroupCustTypeBindingSource.DataSource = dbContext.VodGroupCustTypes.ToList();
vodGroupMediaItemBindingSource.DataSource = dbContext.VodGroupMediaItems.ToList();
}

The various radgridview .datasources are set to one of the BindingSources at runtime.

Thanks,
Craig

0
Petko_I
Telerik team
answered on 30 Aug 2010, 05:34 PM

I have attached an example which shows how to handle the three main operations with the new domain context – update, insert and delete. I have demonstrated how to make modifications which are propagated to the database layer when the Save Changes button is clicked.
The binding source collection is not directly connected to the domain context because the ToList() method creates a new collection separated from the dbContext.VodMediaTypes IQueryable.  
vodMediaTypeBindingSource.DataSource = dbContext.VodMediaTypes.ToList();

The persistent objects that have been obtained from the context can be updated with the same context. The problem comes when you add an item to (or delete an item from) the binding source. As the list is not connected in any way to the context, you need to invoke the Add method with the new item as a parameter. To do this you can handle the ListChanged event of the binding source and check if the type of the operation is addition.  

private void View_DataSourceCollectionChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
{
    if (e.ListChangedType == ListChangedType.ItemAdded)
    {
        BindingSource source = sender as BindingSource;
        if (e.NewIndex >= 0 &&
            e.NewIndex < source.List.Count)
        {
            Category newCategory = source.List[e.NewIndex] as Category;
            this.context.Add(newCategory);
        }
    }
}

The same event can be used to check if an item is deleted but there is not a direct way to obtain a reference to it. In the example I have subscribed to the RowsChanging event of the grid in order to show how to cancel an unintended deletion.

I would like to highlight that with the old scope approach you can use an ObjectView which can greatly simplify your application logic. The downside is you cannot work with the visual designer (if you resort to the classic wizards).

If you need further assistance, do not hesitate to contact us back.


Sincerely yours,
Petko_I
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Getting Started
Asked by
Stefan
Top achievements
Rank 1
Answers by
Damyan Bogoev
Telerik team
Craig Parks
Top achievements
Rank 1
Petko_I
Telerik team
Share this question
or