Hi,All.
My test application describes the problem I have encounter with. (I have attached it )
The application is a form containing an ultra grid bound to an ultra data source.1. I change data in a cell.2 Carriage retains in a cell.3. Then I click on the button 'Close' that has the following code in event handler: private void closeBtn_Click(object sender, EventArgs e) { Close(); }4. OnDataSourceCellDataUpdating is fired.5. Form does not close.(// In OnDataSourceCellDataUpdating I set e.Cancel in True specially to show that form in this case cannot be closed. But I want when an user clicks on 'Close' button Form closes anyway.
Do you have an idea what the problem could be? Thanks in advance.
The problem here seems to be the fact that your button's event handler is never actually being hit, the reason being that the ActiveCell of the grid is still in edit mode. When you try to click on the button, the grid must lose focus before a new control can take focus. As part of this operation, the grid is going to try to commit the change of the cell to the underlying data source; since this operation has failed, the grid doesn't leave edit mode. You might want to take a different approach, such as delaying committing anything to your database until you know that the user isn't closing the form.
-Matt
Matt,
Thanks, for your quickly answering my question.Yes, In debug mode I see that OnClick event handler is never fired if grid doesn't leave edit mode, to be more exact if e.Cancel is set to 'True' in OnDataSourceCellDataUpdating handler. But I hoped an UltraDataSource has any ability to know that something tries to close(dispose, etc) it from outside, like it happens when grid, placed on a form, being disposed during form closing(by clicking 'X' button). Actually I show my form(which I attached to this thread)as modal one from main application form to allow the user to setup some values and then save they to the database by clicking on 'OK' button. I use OnDataSourceCellDataUpdating event to prevent cell from updating and showing some warning message in the corresponding cases. But if user would not continue to work with that form he should have ability to close form regardless of whether grid is in edit mode or not.I found the solution that worked, but it seems it's not so good as I would like.1) I inherited from IMessageFilter and implemented PreFilterMessage(ref Message m) to know about clicking on 'Close' button. I store this fact in StorageForFormCloseActions.FormClosing Property.
public bool PreFilterMessage(ref Message m) { if (m.Msg == (int)WindowsMessages.WM_LBUTTONDOWN) { if (HWNDs.Contains(m.HWnd)) //HWNDs collection contains HWND of 'Close' button. { StorageForFormCloseActions.FormClosing = true;// Now I know that form needs to be closed. return false; } return false; } return false; }
2) On the OnFormLoad event handler of my dialog form I add this MessageFilter to application like this: Application.AddMessageFilter(MyMessageFilter);And also store Close Button's HWND to the corresponding collection.
On FormClosed event handler I removed this MessageFilter from application and reset StorageForFormCloseActions.Current.FormClosing to False.3) Since I stored the fact about clciking on the 'Close' button to bool property I can use that inOnDataSourceCellDataUpdating event handler: if (!StorageForFormCloseActions.Current.FormClosing)// If form is being closed there is no need to update cell's value. { .......... }else{ e.cancel = true; // Here we can close form. // I use BeginInvoke for Close() method to let controls perform its actions.}It would be nice to receive from you comments on this.Thanks in advance.Alex.
'