Datagridview and BO interaction

Datagridview and BO interaction

Old forum URL: forums.lhotka.net/forums/t/1107.aspx


Alex posted on Friday, September 01, 2006

Hi,

I am new to CSLA and to databinding. Just finished reading the book and I am playing with a few simple apps to see if I can get a grasp on the whole approach.

I started out with a simple order entry system but I get lost when I try to respond to user input. For example, when the user inputs a product number, I would like to verify that the product exists and fetch the description and the price automatically and if it does not, I would like to pop a messagebox asking if the product should be created (by popping up the product edit screen).

My first attempt was the CellValidating event but it seems that emitting PropertyChanged events (by modifying properties on the lineitem object) inside the validation handler leads to all sort of quirky behavior from the UI.

Same thing when I try to have a running total on the order. I added a listchanged listener on the lineitems in the invoice, I then update the total and emit a PropertyChanged event for the invoice. This again seems to upset the grid.

Am I approaching this right? Are there restrictions on when it is permissible to modify a business object in response to user interaction (CellValidating,CellParsing...)?

Any help would be greatly appreciated

Thanks

Brian Criswell replied on Saturday, September 02, 2006

What do you mean by "upsets the grid"?

Alex replied on Saturday, September 02, 2006

Hi Brian,

Thanks for answering. Sorry about that, I should of been clearer.

Here is an example of what I mean. I started of with a LineItem class which derives from BusinessBase. It only has 3 properties: id (Guid), code (string) and description (string). The id property is read only and gets assigned a new Guid in the constructor.

I also have a LineItems class which derives from BusinessListBase. I then bind this LineItems to a datagridview using a bindingsource. The grid displays 2 columns (code and description) in textbox columns.

Everything works fine up to this point.

If I add the following in the CellValidating handler for the grid then run the program, type anything in the code column and press down arrow to change records the record we just modified just dissappears from the grid.

if (e.ColumnIndex == 0) {
LineItem item = items[e.RowIndex];
item.Description = "Test";
}

(items is the datasource for the bindingsource)

I think the problem comes from emitting PropertyHasChanged from inside the CellValidating handler because I can modify the description from the Code property setter in the lineitem class without problem.

This handler is just to illustrate the situation, but I still think I need this behavior to be in the UI because I want to interact with the user while performing validation. All of our legacy applications won't let you exit certain fields if they don't contain a valid value and entering valid values in certain fields often leads to various popups to configure various options.

Thanks again for your help

In case it matters, I am using 2.0.3

Brian Criswell replied on Sunday, September 03, 2006

Are there any filters on the grid to hide rows?  Are there any other events handled in the grid or is there any other code in the validating event?  Does the LineItemList still contain the LineItem instance that was edited?

My first guess would be that for some reason the grid is cancelling the AddNew and removing the LineItem, but that is just a guess.

Alex replied on Sunday, September 03, 2006

There are no filters on the grid, no other events handler and that was the complete handler for the validating event.

You are right the row is getting removed from the list.

It would seem that when on a newly created row, emitting a PropertyChanged event inside CellValidating and immediately after changing rows eventually leads to a call to the CurrencyManager's CancelCurrentEdit which calls CancelEdit in BusinessBase.

It would seem that calling DataGridView.CommitEdit before any PropertyChanged events inside the validation handler fixes the problem.

Brian Criswell replied on Sunday, September 03, 2006

That seems like really odd behaviour.  Is your object invalid when you move off the line and  the object is removed?  I have to admit that this is probably getting outside my experience.  You could try the ObjectListView in the CSLAcontrib project as it is designed to buffer changes between the DataGridView and your list, but I am not sure it would help.  Something else may be going on there.  Does anyone else have any ideas?

Copyright (c) Marimer LLC