Menu

Detect When Grid "Dirty"

Help
2007-01-14
2013-04-23
  • Nobody/Anonymous

    What is the simplest way to detect when the grid is "dirty," that is, when the user has edited something, changed the value of a cell, so that on closing the window, form, or dialog on which the grid appears, the user should be prompted whether to save changes or not?

     
    • Davide Icardi

      Davide Icardi - 2007-01-14

      Hi,

      I think that you can define a Controller like this:

              class DirtyController : SourceGrid.Cells.Controllers.ControllerBase
              {
                  public bool IsDirty = false;

                  public override void OnValueChanged(SourceGrid.CellContext sender, EventArgs e)
                  {
                      base.OnValueChanged(sender, e);

                      IsDirty = true;
                  }
              }

      then add it to the grid with this code:

      grid1.Controller.AddController(new DirtyController());

      You can check the DirtyController.IsDirty value to see if the user has changed a value.

      Davide

       
    • Nobody/Anonymous

      Sorry I forgot to specify that I am using .NET 1.1 and hence SourceGrid2. Your posting gave me some leads, and I came up with the following for VB.NET 2003 that is working fine for me. Thank you.

      Public Class DataCellBehavior
         Implements SourceGrid2.BehaviorModels.IBehaviorModel

      #Region " Steps to implement grid cell behavior "

         ' steps to get a behavior model to operate for a cell
         '
         '  1. create this class; name could be something else; named DataCellBehavior in
         '     example here to distinguish behavior for data cells from behavior for
         '     header cells; define a separate class with different behavior for header
         '     cells if desired
         '
         '  2. implement SourceGrid2.BehaviorModels.IBehaviorModel in 1st line in this class;
         '     when the foregoing implementation line is written, the IDE adds most of the
         '     property and event handler routines below automatically
         '
         '  3. declare Public bmData As New DataCellBehavior; or as this class by whatever other
         '     other name; variable bmData could be named something else; named in example
         '     here with the word Data in bmData to distinguish behavior for data cells from
         '     behavior for header cells
         '
         '  4. when loading grid or creating cells, set the behavior of a cell as in
         '     g(row, 1).Behaviors.Add(bmData), where Dim g As SourceGrid2.Grid
         '
         '        Dim typeString As Type = GetType(String) ' needed to make cells editable
         '        For row = 1 To gDBE.NumFields   ' row 0 is header created in earlier code
         '           g.Rows.Insert(row)
         '           g(row, 0) = New SourceGrid2.Cells.Real.Cell(pFieldNames(row - 1))
         '           g(row, 1) = New SourceGrid2.Cells.Real.Cell(pFieldValues(row - 1), typeString)
         '           g(row, 0).VisualModel = vmRowHeader
         '           g(row, 1).VisualModel = vmData
         '           g(row, 1).Behaviors.Add(bmData) ' <<<---- activate behavior class for this cell
         '        Next
         '
         '  5. in CanReceiveFocus below, add Return True, or else the cell cannot be entered
         '     for editing
         '
         '  6. add public property IsDirty as boolean and corresponding private member
         '     variable mIsDirty so that appropriate event handler created by Implements can
         '     set IsDirty to true, and other code can check value of IsDirty

      #End Region

      #Region " Manually added members and routines "

         Private mIsDirty As Boolean   ' this member added manually

         Public Property IsDirty() As Boolean
            ' this property added manually, not generated by Implements statement
            Get
               Return mIsDirty
            End Get
            Set(ByVal Value As Boolean)
               mIsDirty = Value
            End Set
         End Property

      #End Region

      #Region " Routines automatically generated by Implements statement "

         Public ReadOnly Property CanReceiveFocus() As Boolean Implements SourceGrid2.BehaviorModels.IBehaviorModel.CanReceiveFocus
            Get
               Return True
            End Get
         End Property

         Public Sub OnClick(ByVal e As SourceGrid2.PositionEventArgs) Implements SourceGrid2.BehaviorModels.IBehaviorModel.OnClick

         End Sub

         Public Sub OnContextMenuPopUp(ByVal e As SourceGrid2.PositionContextMenuEventArgs) Implements SourceGrid2.BehaviorModels.IBehaviorModel.OnContextMenuPopUp

         End Sub

         Public Sub OnDoubleClick(ByVal e As SourceGrid2.PositionEventArgs) Implements SourceGrid2.BehaviorModels.IBehaviorModel.OnDoubleClick

         End Sub

         Public Sub OnEditEnded(ByVal e As SourceGrid2.PositionCancelEventArgs) Implements SourceGrid2.BehaviorModels.IBehaviorModel.OnEditEnded

         End Sub

         Public Sub OnEditStarting(ByVal e As SourceGrid2.PositionCancelEventArgs) Implements SourceGrid2.BehaviorModels.IBehaviorModel.OnEditStarting
            mIsDirty = True
            ' maybe not the best place to set mIsDirty to True since there might
            ' not have been any actual editing yet, but seems to provide the best
            ' behavior among the choices
         End Sub

         Public Sub OnFocusEntered(ByVal e As SourceGrid2.PositionEventArgs) Implements SourceGrid2.BehaviorModels.IBehaviorModel.OnFocusEntered

         End Sub

         Public Sub OnFocusEntering(ByVal e As SourceGrid2.PositionCancelEventArgs) Implements SourceGrid2.BehaviorModels.IBehaviorModel.OnFocusEntering

         End Sub

         Public Sub OnFocusLeaving(ByVal e As SourceGrid2.PositionCancelEventArgs) Implements SourceGrid2.BehaviorModels.IBehaviorModel.OnFocusLeaving
        
         End Sub

         Public Sub OnFocusLeft(ByVal e As SourceGrid2.PositionEventArgs) Implements SourceGrid2.BehaviorModels.IBehaviorModel.OnFocusLeft

         End Sub

         Public Sub OnKeyDown(ByVal e As SourceGrid2.PositionKeyEventArgs) Implements SourceGrid2.BehaviorModels.IBehaviorModel.OnKeyDown

         End Sub

         Public Sub OnKeyPress(ByVal e As SourceGrid2.PositionKeyPressEventArgs) Implements SourceGrid2.BehaviorModels.IBehaviorModel.OnKeyPress

         End Sub

         Public Sub OnKeyUp(ByVal e As SourceGrid2.PositionKeyEventArgs) Implements SourceGrid2.BehaviorModels.IBehaviorModel.OnKeyUp

         End Sub

         Public Sub OnMouseDown(ByVal e As SourceGrid2.PositionMouseEventArgs) Implements SourceGrid2.BehaviorModels.IBehaviorModel.OnMouseDown

         End Sub

         Public Sub OnMouseEnter(ByVal e As SourceGrid2.PositionEventArgs) Implements SourceGrid2.BehaviorModels.IBehaviorModel.OnMouseEnter

         End Sub

         Public Sub OnMouseLeave(ByVal e As SourceGrid2.PositionEventArgs) Implements SourceGrid2.BehaviorModels.IBehaviorModel.OnMouseLeave

         End Sub

         Public Sub OnMouseMove(ByVal e As SourceGrid2.PositionMouseEventArgs) Implements SourceGrid2.BehaviorModels.IBehaviorModel.OnMouseMove

         End Sub

         Public Sub OnMouseUp(ByVal e As SourceGrid2.PositionMouseEventArgs) Implements SourceGrid2.BehaviorModels.IBehaviorModel.OnMouseUp

         End Sub

         Public Sub OnValueChanged(ByVal e As SourceGrid2.PositionEventArgs) Implements SourceGrid2.BehaviorModels.IBehaviorModel.OnValueChanged
            ' not necessarily the right place to set mIsDirty to True because this event
            ' apparently can be fired by just navigating to another record
         End Sub

      #End Region

      End Class

      T. R. Halvorson
      Sidney, Montana

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.