SourceGrid specification and testing: Difference between revisions

From OpenPetra Wiki
Jump to navigation Jump to search
(Fixes and Changes to the Grid Control's Behaviour)
No edit summary
Line 101: Line 101:
     }
     }


----


Basically, the supporting code needs to identify the user action and behave accordingly, this includes selecting, sorting, adding and deleting rows. The InvokeFocusedRowChanged() method allows the calling of the FocusedRowChanged event code when it needs to be called manually.
Basically, the supporting code needs to identify the user action and behave accordingly, this includes selecting, sorting, adding and deleting rows. The InvokeFocusedRowChanged() method allows the calling of the FocusedRowChanged event code when it needs to be called manually.


----
----
Line 168: Line 165:
     }
     }


----


The above event ensures the correlation between the FCurrentRow variable and the current row, especially during sorting etc.
The above event ensures the correlation between the FCurrentRow variable and the current row, especially during sorting etc.
Line 230: Line 226:
         }
         }
     }
     }
----


You will notice that the grid's DataSource needed to be set to Null before being reset. This ensured removal of focus from a cell on the previous row. The code also invokes the FocusedRowChanged event as well as sending focus to the new row irrespective of sorting and then gives focus to the key value field and populates the description field if it exists.
You will notice that the grid's DataSource needed to be set to Null before being reset. This ensured removal of focus from a cell on the previous row. The code also invokes the FocusedRowChanged event as well as sending focus to the new row irrespective of sorting and then gives focus to the key value field and populates the description field if it exists.
Line 242: Line 235:
'''Saving Rows'''
'''Saving Rows'''
The main issue with saving was ensuring that the sorting was correct after changing the key field. To do this the following code was added to the save event in the template:
The main issue with saving was ensuring that the sorting was correct after changing the key field. To do this the following code was added to the save event in the template:
----


//The sorting will be affected when a new row is saved, so need to reselect row
//The sorting will be affected when a new row is saved, so need to reselect row
Line 294: Line 290:
     */
     */


----


The above code ensures that the row at the same index position is selected after the deletion occurs and the missing FocusedRowChanged event is fired.
The above code ensures that the row at the same index position is selected after the deletion occurs and the missing FocusedRowChanged event is fired.


When we do come to add autogenerated DeleteCurrent{#DETAILTABLE}() code to the templates, we will need the above code to ensure correct grid row behaviour, as well as a call to DeleteRowManual() in the manual code file and the ability to offer a dioalog box to confirm deletion and to supply a meaningful message.
When we do come to add autogenerated DeleteCurrent{#DETAILTABLE}() code to the templates, we will need the above code to ensure correct grid row behaviour, as well as a call to DeleteRowManual() in the manual code file and the ability to offer a dioalog box to confirm deletion and to supply a meaningful message.
'''Grid Testing'''
Testing needs to be setup where we can test the grid on all its basic functions in forms derived from each of the templates.
Currently, all code generated from the templates compiles and a number of basic maintain screens have been tested for correct behaviour, but a smaller stand-alone application will also be needed to test the behaviour in isolation.

Revision as of 01:25, 10 July 2012

The SourceGrid control and its implementation in OpenPetra, has required that modifications be made to its event handling to ensure consistent and predictable event firing in relation to selecting, adding, deleting, sorting and saving rows in the grid. This includes detecting and cancelling repating and unwanted events (mainly FocusRowLeaving event) and invoking events (mainly FocusedRowChanged) when no event is fired (when it should be). Below are the changes that have been introduced to the grid, the grid wrapper and the templates that affect use of the grid throughtout OpenPetra.

'''Event Handling''' The two events that have been the focus of the changes to the grid-related code are FocusRowLeaving (when the currently selected row loses focus) and FocusRowChanged (when a new row receives focus).

Event - Keyboard handling The grid fires row-level events when the grid itself loses focus even though the current row had not changed. I remmed out two lines of code in the SourceGrid code that related to the user tabbing away or clicking away from the entire grid itself to stop the firing of these events. No other in-grid events are affected.

Event - FocusRowLeaving This is the even that fires most often and needs to be curtailed and checked for repeats. Here's the code from the template with supporting methods etc.:



   private bool firstFocusEventHasRun = false;
   private bool isRepeatLeaveEvent = false;
   private int gridRowsCount = 0;
   private int numGridRows     = 0;
   private int gridRowsCountHasChanged = 0;
   private bool newFocusEventStarted = false;
   private void FocusPreparation(bool AIsLeaveEvent)
   {
       if (isRepeatLeaveEvent)
       {
           return;
       }
       numGridRows = grdDetails.Rows.Count;
       //first run only
       if (!firstFocusEventHasRun)
       {
           firstFocusEventHasRun = true;
           gridRowsCount = numGridRows;
       }
       //Specify if it is a row change, add or delete
       if (gridRowsCount == numGridRows)
       {
           gridRowsCountHasChanged = 0;
       }
       else if (gridRowsCount > numGridRows)
       {
           gridRowsCount = numGridRows;
           gridRowsCountHasChanged = -1;
       }
       else if (gridRowsCount < numGridRows)
       {
           gridRowsCount = numGridRows;
           gridRowsCountHasChanged = 1;
       }
   }
   private void InvokeFocusedRowChanged(int AGridRowNumber)
   {
       SourceGrid.RowEventArgs rowArgs  = new SourceGrid.RowEventArgs(AGridRowNumber);
       FocusedRowChanged(grdDetails, rowArgs);
   }
   private void FocusRowLeaving(object sender, SourceGrid.RowCancelEventArgs e)
   {
       //Ignore this event if currently sorting
       if (grdDetails.Sorting)
       {
           newFocusEventStarted = false;
           return;
       }
       if (newFocusEventStarted == false)
       {
           newFocusEventStarted = true;
       }
       FocusPreparation(true);
       if (!isRepeatLeaveEvent)
       {
           isRepeatLeaveEvent = true;
           if (gridRowsCountHasChanged == -1 || numGridRows == 2)  //do not run validation if cancelling current row
                                                                   // OR only 1 row present so no rowleaving event possible
           {
               e.Cancel = true;
           }
           Console.WriteLine("FocusRowLeaving");
           if (!ValidateAllData(true, true))
           {
               e.Cancel = true;
           }
       }
       else
       {
           // Reset flag
           isRepeatLeaveEvent = false;
           e.Cancel = true;
       }
   }


Basically, the supporting code needs to identify the user action and behave accordingly, this includes selecting, sorting, adding and deleting rows. The InvokeFocusedRowChanged() method allows the calling of the FocusedRowChanged event code when it needs to be called manually.


   private void FocusedRowChanged(System.Object sender, SourceGrid.RowEventArgs e)
   {
       newRecordUnsavedInFocus = false;
       isRepeatLeaveEvent = false;
       if (!grdDetails.Sorting)
       {
           //Sometimes, FocusedRowChanged get called without FocusRowLeaving
           //  so need to handle that
           if (!newFocusEventStarted)
           {
               //This implies start of a new event chain without a previous FocusRowLeaving
               FocusPreparation(false);
           }
           //Only allow, row change, add or delete, not repeat events from grid changing focus
           if(e.Row != FCurrentRow && gridRowsCountHasChanged == 0)
           {
               // Transfer data from Controls into the DataTable
               if (FPreviouslySelectedDetailRow != null)
               {
                   GetDetailsFromControls(FPreviouslySelectedDetailRow);
               }
               // Display the details of the currently selected Row
               FPreviouslySelectedDetailRow = GetSelectedDetailRow();
               ShowDetails(FPreviouslySelectedDetailRow);
               pnlDetails.Enabled = true;
           }
           else if (gridRowsCountHasChanged == 1) //Addition
           {
           }
           else if (gridRowsCountHasChanged == -1) //Deletion
           {
               if (numGridRows > 1) //Implies at least one record still left
               {
                   // Select and display the details of the currently selected Row without causing an event
                   grdDetails.SelectRowInGrid(e.Row, TSgrdDataGrid.TInvokeGridFocusEventEnum.NoFocusEvent);
                   FPreviouslySelectedDetailRow = GetSelectedDetailRow();
                   ShowDetails(FPreviouslySelectedDetailRow);
                   pnlDetails.Enabled = true;
               }
               else
               {
                   FPreviouslySelectedDetailRow = null;
                   pnlDetails.Enabled = false;
               }
           }
       }
       FCurrentRow = e.Row;
       //Event chain tidy-up
       gridRowsCountHasChanged = 0;
       newFocusEventStarted = false;
   }


The above event ensures the correlation between the FCurrentRow variable and the current row, especially during sorting etc.

Adding Rows Where the form templates contain code for adding records to the grid, changes have had to be made to control event firing. Here is an example:



   private bool newRecordUnsavedInFocus = false;
   public bool CreateNew{#DETAILTABLE}()
   {
       if(ValidateAllData(true, true))
       {    
           int previousGridRow = grdDetails.Selection.ActivePosition.Row;
           {#DETAILTABLE}Row NewRow = FMainDS.{#DETAILTABLE}.NewRowTyped();
           {#INITNEWROWMANUAL}
           FMainDS.{#DETAILTABLE}.Rows.Add(NewRow);
           
           FPetraUtilsObject.SetChangedFlag();

grdDetails.DataSource = null;

           grdDetails.DataSource = new DevAge.ComponentModel.BoundDataView(FMainDS.{#DETAILTABLE}.DefaultView);
           

SelectDetailRowByDataTableIndex(FMainDS.{#DETAILTABLE}.Rows.Count - 1);

           InvokeFocusedRowChanged(grdDetails.SelectedRowIndex());
           //Must be set after the FocusRowChanged event is called as it sets this flag to false
           newRecordUnsavedInFocus = true;
           FPreviouslySelectedDetailRow = GetSelectedDetailRow();
           ShowDetails(FPreviouslySelectedDetailRow);
           Control[] pnl = this.Controls.Find("pnlDetails", true);
           if (pnl.Length > 0)
           {

//Look for Key & Description fields bool keyFieldFound = false; foreach (Control detailsCtrl in pnl[0].Controls) { if (!keyFieldFound && (detailsCtrl is TextBox || detailsCtrl is ComboBox)) { keyFieldFound = true; detailsCtrl.Focus(); }

if (detailsCtrl is TextBox && detailsCtrl.Name.Contains("Descr") && detailsCtrl.Text == string.Empty) { detailsCtrl.Text = "PLEASE ENTER DESCRIPTION"; break; } }

           }
           return true;
       }
       else
       {
           return false;
       }
   }

You will notice that the grid's DataSource needed to be set to Null before being reset. This ensured removal of focus from a cell on the previous row. The code also invokes the FocusedRowChanged event as well as sending focus to the new row irrespective of sorting and then gives focus to the key value field and populates the description field if it exists.

Also in another template method: SelectDetailRowByDataTableIndex(), the last line is changed that calls a new signature method in the wrapper that selects the specified row but without firing any events. The enumerator allows you to specify the event you would like to fire.

grdDetails.SelectRowInGrid(RowNumberGrid, TSgrdDataGrid.TInvokeGridFocusEventEnum.NoFocusEvent);

Saving Rows The main issue with saving was ensuring that the sorting was correct after changing the key field. To do this the following code was added to the save event in the template:



//The sorting will be affected when a new row is saved, so need to reselect row if (newRecordUnsavedInFocus) { SelectDetailRowByDataTableIndex(FMainDS.{#DETAILTABLE}.Rows.Count - 1); InvokeFocusedRowChanged(grdDetails.SelectedRowIndex()); }

Even though the FocusRowChanged event should not be needed, it is used to display the record (which may have moved due to sorting) and keep the FCurrent variable up to date.

Deleting Rows The code for deleting rows is yet to be added to the templates, but I have the following code that is used in GL Batch in the manual code file:



private void CancelRow(System.Object sender, EventArgs e) {

   if (FPreviouslySelectedDetailRow == null)
   {
       return;
   }
   int newCurrentRowPos = TFinanceControls.GridCurrentRowIndex(grdDetails);
   /*
   Code block here that deals with cancelling the underlying records
   */
   //If some row(s) still exist after deletion
   if (grdDetails.Rows.Count > 1)
   {
       //If last row just deleted, select row at old position - 1
       if (newCurrentRowPos == grdDetails.Rows.Count)
       {
           newCurrentRowPos--;
       }
   }
   else
   {
       EnableButtonControl(false);
       ClearDetailControls();
       newCurrentRowPos = 0;
   }
   //Select and call the event that doesn't occur automatically
   InvokeFocusedRowChanged(newCurrentRowPos);
   /*
   additional code
   */


The above code ensures that the row at the same index position is selected after the deletion occurs and the missing FocusedRowChanged event is fired.

When we do come to add autogenerated DeleteCurrent{#DETAILTABLE}() code to the templates, we will need the above code to ensure correct grid row behaviour, as well as a call to DeleteRowManual() in the manual code file and the ability to offer a dioalog box to confirm deletion and to supply a meaningful message.

Grid Testing Testing needs to be setup where we can test the grid on all its basic functions in forms derived from each of the templates.

Currently, all code generated from the templates compiles and a number of basic maintain screens have been tested for correct behaviour, but a smaller stand-alone application will also be needed to test the behaviour in isolation.