unbound columns values are gone!!

Posts   
 
    
hfsyria
User
Posts: 6
Joined: 22-Mar-2010
# Posted on: 06-Apr-2010 15:19:26   

hi

i have a form that contains some controls that describe an order and a grid that contains order details

i used an orderCollection and bindingSource i set the bindingSource datasource Property to orderCollection i bound the controls in the forms seperatlly to the fields of the collection though bindingSource then i set the datagridview dataSource property to the same bindingSource and set it's DataMember Property to OrderDetail

when i save the order i use the following code


orderCollection.SaveMulti(tru);

i have some unbound columns in the grid, i calculate the values based on values of other column

the problem is that whenever i change any thing in the orderCollection or even save it the unbound columns values are goneconfused

examples:


orderCollection[0].OrderDate = DateTime.Now;
orderCollection.SaveMulti(true);

each row of these will cause the unboud column value to disappear note: the orderCollection always contain one item

what could be the problem

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 06-Apr-2010 15:24:09   
Frans Bouma | Lead developer LLBLGen Pro
hfsyria
User
Posts: 6
Joined: 22-Mar-2010
# Posted on: 06-Apr-2010 15:49:35   

LLBLGen Pro. Version:2.6 Final file version: 2.6.8.804 i'm using Selfservicing for .NET2 i'm using SQL Server 2005

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 06-Apr-2010 22:00:05   

I'm guessing that changing something on the entity is causing the grid to refresh the row, effectivly resetting the unbound value.

Which grid are you using...?

Most of them have a row_updated event, or something similar which you can use to refresh unbound values.

Another approach is to add properties to the entities to contain the calculated values, and to bind the grid to those instead of unusing unbound columns.

Matt

hfsyria
User
Posts: 6
Joined: 22-Mar-2010
# Posted on: 07-Apr-2010 12:15:44   

MTrinder wrote:

I'm guessing that changing something on the entity is causing the grid to refresh the row, effectivly resetting the unbound value.

the problem is that it refresh the whole grid not just one row

MTrinder wrote:

Which grid are you using...?

Most of them have a row_updated event, or something similar which you can use to refresh unbound values.

i'm using devcomponents grid and recalculating the values are not good for me, becasue it means connecting to database many times in some tables i have three unbound columns

MTrinder wrote:

Another approach is to add properties to the entities to contain the calculated values, and to bind the grid to those instead of unusing unbound columns.

i'll try your approach, but do you mean adding custom properties in designer or adding properties to the entity classes in code

even if your solutions work for me, at least i want to know why this happens because when i bind a single collection to a table the values of unbound columns never changes unless i clear the collection

this problem only happens when i'm using a master-detail binding

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 07-Apr-2010 13:24:04   

I couldn't reproduce this. Where exactly do you set the values of the unbound columns?

I added a Total column to the DataGridView showing OrderDetails from Northwind. And here is my code that sets the unbound column.

        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            for (int i = 0; i < e.RowCount; i++)
            {
                var row = dataGridView1.Rows[e.RowIndex +i];
                if((row.Cells[2].Value != null)&& (row.Cells[3].Value != null))
                {
                    row.Cells[5].Value = (decimal.Parse(row.Cells[2].Value.ToString())*
                                         decimal.Parse(row.Cells[3].Value.ToString())).ToString();
                }
            }
        }

I used an OrdersCollection with a bindingSource and the Grid was bound to the "Orderdetails" DataMember. And on some button event I tried changing a field of the corresponding Order entity or saving the OrderEntity, and still the unbound columns retained their values.

Btw, I'm using the default MS DataGridView, could you please try it out to validate my output.

Thanks.

hfsyria
User
Posts: 6
Joined: 22-Mar-2010
# Posted on: 07-Apr-2010 16:22:50   

i've added an attachment for a test form i used the form with the Example_NorthwindCS1_06062008 example

i think you will see the problem clearly

if you press the button and changed the field by code the unboud column values will vanish if you change any field value from textboxes the unboud column values will vanish

Attachments
Filename File size Added on Approval
testForm.rar 4,345 07-Apr-2010 16:23.02 Approved
Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 07-Apr-2010 17:43:16   

What happens is that when the dataSource bound to the binding source gets dirty, the bindingSource re-binds to the Grid Control, causing the Rows to be re-added to Grid.

SO you have 2 options, either add a property to the underlying entity to hold the calculated value to be retained when refreshing the Grid.

Or use the _RowsAdded Event to add the code which populates the unbound Column. Or call the same method that populates the calculated column when you save or change a field of the underlying datasource.

To check on me just handle the _RowsAdded Event, and place a break point and see that it gets called when you refresh the underlying dataSource.

hfsyria
User
Posts: 6
Joined: 22-Mar-2010
# Posted on: 07-Apr-2010 18:26:38   

i get it now, it's just as you said but why this strange behavior

and why only happens when there a master-detail binding

thanks for reply

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 08-Apr-2010 12:15:54   

No idea.

hfsyria
User
Posts: 6
Joined: 22-Mar-2010
# Posted on: 08-Apr-2010 13:36:08   

at first i thought it's an llblgen related issue

after some searching and testing i found a way to solve my problem without repopulating the values each time a field changes

I used DataSourceUpdateMode.OnPropertyChanged for contols bound to master side fields which are order fields

then i set orderBindingSource.RaiseListChangedEvents to false after setting to false and changing any field, unboud column values persist in the grid

and before saving i call orderBindingSource.EndEdit()

the thing i'm not sure about is that if setting this property to false has any negative aspects or effects

also, i searched the net i couldn't find any resource or inforamtion if the bindingsource uses the ListChanges event enternally

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 12-Apr-2010 09:09:01   

Thanks for the feedback.

and before saving i call orderBindingSource.EndEdit()

the thing i'm not sure about is that if setting this property to false has any negative aspects or effects

I don't think so.