Newbie: Update single Entity in Collection resp. single row in DataGridView

Posts   
 
    
softwarea
User
Posts: 57
Joined: 05-Mar-2007
# Posted on: 30-Jul-2007 18:24:26   

Hi,

I' not sure if this is a LLBL matter or a DataGridView problem or something completely different. Anyway, I'm stuck and hope someone can give me a tipp:

I'm developing a WinForms C# application with LLBL 2.0 (SelfService).

On one of the forms I have a DataGridView which I bind to an EntityCollection "AllCustomers" during run-time. The collection receives it's data fom a View which shows customer names, addresses and similar stuff from joined tables like <Customers>, <Orders> and <Status>.

Whenever the user selects a customer in the DataGridView, I want the user to be able to edit the customer's data (but only CustomerEntity). So I fetch the corresponding entity from the database like CustomersEntity selectedCustomer = new CustomerEntity(xxx); and write all relevant data in some somple TextBoxes and ComboBoxes.

After modifing the data the user clicks on a Save-Button and I simply transfer all TextBox contents to the selectedCustomer entity and execute a selectedCustomer.Save().

So far, so good.

Of course I would like the DataGridView to reflect the changes made to the selected customer. And I could do that by simply reloading the whole EntityCollection "AllCustomers". But:

  1. I'm loosing the selected row.
  2. The refresh takes very long, because their are so many customers in the collection.

So, is there a way, to simply refresh ONE single row in a DataGidView (resp. refresh one single Entity in an EntityCollection and the tell the DataGridView to refresh only the corresponding row)?

I hope, all this was not too confusing and someone knows a simple solution (unfortunately I'm not such an experienced developer...:-( ).

Thanks, Ingmar

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 31-Jul-2007 00:57:20   

Hi,

I am not aware of an easy way to selectively refresh a single row unless you do it manually. Now you may try to keep and maintain the collection directly to avoid the whole refetch, or to use DataSource Controls which automate the whole thing.

softwarea
User
Posts: 57
Joined: 05-Mar-2007
# Posted on: 31-Jul-2007 12:18:29   

Hi Jessyno,

thank you for your answer!

Well, due to some reasons I do not want to use the LLBL DataSource Controls, but I'm interested in what exactly you mean by "refreshing a row manually". Because that is more or less exactly what I intend to do. It's just that I do now know how to do that :-(

Hope you can give me a tipp...though this might have nothing to do with LLBL...

Thanks! Ingmar

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39616
Joined: 17-Aug-2003
# Posted on: 31-Jul-2007 17:43:45   

softwarea wrote:

Hi,

I' not sure if this is a LLBL matter or a DataGridView problem or something completely different. Anyway, I'm stuck and hope someone can give me a tipp:

I'm developing a WinForms C# application with LLBL 2.0 (SelfService).

On one of the forms I have a DataGridView which I bind to an EntityCollection "AllCustomers" during run-time. The collection receives it's data fom a View which shows customer names, addresses and similar stuff from joined tables like <Customers>, <Orders> and <Status>.

Whenever the user selects a customer in the DataGridView, I want the user to be able to edit the customer's data (but only CustomerEntity). So I fetch the corresponding entity from the database like CustomersEntity selectedCustomer = new CustomerEntity(xxx); and write all relevant data in some somple TextBoxes and ComboBoxes.

After modifing the data the user clicks on a Save-Button and I simply transfer all TextBox contents to the selectedCustomer entity and execute a selectedCustomer.Save().

Shouldn't you do yourself a favor and let databinding do this for you? simple_smile

So far, so good.

Of course I would like the DataGridView to reflect the changes made to the selected customer. And I could do that by simply reloading the whole EntityCollection "AllCustomers". But:

  1. I'm loosing the selected row.
  2. The refresh takes very long, because their are so many customers in the collection.

So, is there a way, to simply refresh ONE single row in a DataGidView (resp. refresh one single Entity in an EntityCollection and the tell the DataGridView to refresh only the corresponding row)?

I hope, all this was not too confusing and someone knows a simple solution (unfortunately I'm not such an experienced developer...:-( ).

Thanks, Ingmar

You can do a couple of things: - you can simply use the customer entity object of the collection in the grid and use that one to bind to the textboxes. This will be the least amount of work, as the data is already loaded + you're working on the same object so changes to the entity object are reflected in the grid.

  • you can use a context. Add the collection to the Context object and then after you've loaded the customer entity, you 'get' the customer from the context by using the Get method on the context. This will give you the same instance back as used in the grid so you're effectively working on the same object.

Though I'd go for option 1. simple_smile

For an example, please check the Northwind example on our website (in the examples section) which shows you how to do databinding and keeping everything in sync. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
softwarea
User
Posts: 57
Joined: 05-Mar-2007
# Posted on: 31-Jul-2007 23:39:41   

Hi Otis,

looking at the Northwind example I see the advantage of databinding. And it seems to be pretty easy and advantageous. But I'm not sure if databinding would solve my current problem...

The thing is that the grid is bound to a collection which is based on a multi table join. That means: this join contains fields of the <customer> table (but not all of them) and some fields of other related tables. So I figure that the resulting collection does NOT contain complete customer entities...i.e. is NOT a real CustomerCollection.

So, I still have the problem; The grid is based on a collection which is not fully <customer> entity compatible. On the other hand I have a detail view which let's me edit a real <customer> entity. And if I save a concrete <customer> entity, I assume that neither the collection nor the grid are updated to the modified data...or is this assumption wrong?

So, I'm still looking for a way to refresh a certain entity in the collection (resp. a certain row in the grid). But maybe I still misunderstand the databinding abilities?

Ingmar

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 01-Aug-2007 10:31:13   

Well, due to some reasons I do not want to use the LLBL DataSource Controls

You are developing a Win App, and LLBLGenProDataSource(2) is for Web Apps. Having said that, in win forms you can use a BindingSource object/control, this will make 2-way databinding easier.

Now back to your issue, I think you should try the following: **Solution #1 **(Trying to refresh a single row) - After saving an entity in the DetailsView, pick a reference to the selected entity from the entityCollection bound to the Grid. - Re-fetch this entity. And that's it.

**Solution #2 **(Keep the row selected) - After saving an entity in the DetailsView, pick the index of the selected row from the Grid - Re-fetch the entire collection bound to the dataGrid -Re-Select the Row with the previously saved index.

softwarea
User
Posts: 57
Joined: 05-Mar-2007
# Posted on: 01-Aug-2007 17:23:34   

Hello Walaa,

thank you for yor suggestions too!

Concerning your Solution #1: Yes, that's more or less what I already tried:

foreach (DataGridViewRow row in gridCustomers.Rows)
{
    ViewCustomersEntity visitedCustomer = (ViewCustomersEntity)grCustomers.Rows[row.Index].DataBoundItem;
    if (visitedCustomer.Con_Id == currentCustomerID)
    {
        visitedCustomer .Refetch();
    }
}

First I iterate through all rows in the grid and try to find the primary key of the edited customer (long currentCustomerID). If I found a DataBoundItem with the sam ID I refetch the corresonding entity in the bound collection. I'm sure there is an easier or more efficient approach to identfy the "to-be-refetched" entity in the Collection, but my way should work, right? Anyway, it does not. The data in the grid is unchanged. Any idea what I'm doing wrong?

Concerning your Solution #2: That is what I do not want to do, because the view returns so much data that the performance is really slow. That is exactly the reason why I want to refresh ONE row only.

Thanks as always, Ingmar

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 01-Aug-2007 17:38:30   

Anyway, it does not. The data in the grid is unchanged. Any idea what I'm doing wrong?

Would you please try to use BindingSource to act as the middle man between the Grid and the entityCollection?