Solved

C# WinForm-Datagrid Validating Event

Posted on 2004-10-22
4,673 Views
Last Modified: 2012-06-21
I have a C# Windows form with a datagrid control. There is a Validating event on the datagrid that checks for changes:
  if (this.dsAccts1.HasChanges() == true)
  {
    if (MessageBox.Show("Do you want to save your changes?",
            "Save",
            System.Windows.Forms.MessageBoxButtons.YesNo,
            MessageBoxIcon.Question,MessageBoxDefaultButton.Button1)
      == System.Windows.Forms.DialogResult.Yes)
      {
           e.Cancel = true;
      }
      else
      {
          //do nothing for now
      }
  }

When I update the value of a cell, move to a next record, then scroll, the Validating event is firing when that updated cell scrolls off of the datagrid's visible area.
I have also noticed that when using the mouse scroll wheel, the grid stops scrolling when the active record leave the viewable area.
QUESTION: How do I prevent the Validating event from firing while I scroll the grid???
0
Question by:Jokra_the_Barbarian
    4 Comments
     
    LVL 28

    Expert Comment

    by:mmarinov
    Hi Jokra_the_Barbarian,

    here is the remarks from the msdn about validating event
    Remarks
    When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:

    Enter
    GotFocus
    Leave
    Validating
    Validated
    LostFocus
    When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:

    Enter
    GotFocus
    LostFocus
    Leave
    Validating
    Validated
    If the CausesValidation property is set to false, the Validating and Validated events are suppressed.

    If the Cancel property of the CancelEventArgs object is set to true in the Validating event delegate, all events that would normally occur after the Validating event are suppressed.


    i think that set causesvalidation=false and at the end set it to true ( before update ) is the best choise

    B..M
    0
     
    LVL 11

    Author Comment

    by:Jokra_the_Barbarian
    Thank you B..M for your response. I don't think it is related to the Validation as it is the record moving off the viewable area of the datagrid. I found some code on windowsforms.net that takes care of the mouse wheel:
    --------------------------------
      myDataGrid.MouseWheel += new MouseEventHandler(myDataGrid_MouseWheel);

    then:

      private void myDataGrid_MouseWheel(object sender, MouseEventArgs e)
      {
      myDataGrid.Select();
      }
    --------------------------------

    I'm attempting to add an event handler for the datagrid's scroll, but with the following code I'm getting an error:
     
      this.dgGLAccts.Scroll += new System.Windows.Forms.ScrollEventHandler(myDataGrid_ScrollBar);

    ERROR: Cannot implicitly convert type 'System.Windows.Forms.ScrollEventHandler' to 'System.EventHandler'

    Any ideas?
    0
     
    LVL 28

    Accepted Solution

    by:
    Jokra_the_Barbarian,


    it must be
    this.dgGLAccts.Scroll += new EventHandler(myDataGrid_ScrollBar);


    B..M
    0
     
    LVL 11

    Author Comment

    by:Jokra_the_Barbarian
    B..M, thanks for your help. I'll give that EventHandler a try tomorrow.  -Jokra
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Shellfire Box VPN + Lifetime Subscription

    The Shellfire Box easily connects all of your devices, even those that don't offer the possibility to establish a safe vpn connection. Access blocked content and surf safely, no matter where in the world you are located.

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    933 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now