none
DataGridView RowVlaidating fires on form load... RRS feed

  • Question

  • I am trying to validate the data on a row of a DataGridView by using the RowValidating event.

    The problem I am having is that it fires when the form loads and any existing rows that fail my tests cause an error when my code hits e.Cancel = true;

    What is the workaround for this?

    Thanks!


    Newark IT Guy...

    Wednesday, July 24, 2013 5:14 PM

Answers

  • Damon,

    Alright.  First, I put a public static bool in the form class as thus:

          public static bool loading = true;

    At the top of any DataGridView Row validating event, I have put this:

           if( loading == true)
            return;

    Then. at the end of the form load event, I placed:

           loading = false;

    This bypasses any validation on form load, but allows the code to function thereafter.


    Newark IT Guy...

    Thursday, July 25, 2013 10:12 AM

All replies

  • Hi,

    It would be better if you can provide your code here,then I can provide further suggestions for you.

    Now,I give you a sample about using the RowValidating event.I hope it can help you !

     private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
            {
                // Calculate the customer ID for offline mode. Currently this is done by handling
                // the RowValidating event, but note that this event fires each time focus leaves 
                // a row. Currently the row index has to be special cased to decide when to perform
                // calculate the customerID.            
                if ((e.RowIndex == (dataGridView1.Rows.Count - 2)) &&
                    dataGridView1.Rows[e.RowIndex + 1].IsNewRow)
                {
                    DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
                    // Only want to calculate the CustomerID if it hasn't been calculated.
                    if (row.Cells["CustomerID"].Value != null)
                    {
                        if (row.Cells["CustomerID"].Value == DBNull.Value ||
                            row.Cells["CustomerID"].Value.ToString() == "<AUTO>")
                        {
                            // CustomerID is calculated by taking the first 2 characters 
                            // of the Company name and appending the RowIndex. 
                            string coname = row.Cells["Company"].Value as string;
                            if (coname != null)
                            {
                                string coid = coname.ToUpper().Substring(0, Math.Min(2, coname.Length)) + e.RowIndex.ToString() ;
                                row.Cells["CustomerID"].Value = coid;
                                e.Cancel = false;
                            }
                        }
                    }
                }
            }

    You can also consider posting it in Windows Form forum(http://social.msdn.microsoft.com/Forums/en-US/home?category=windowsforms) for more efficient responses.

    Have a nice day!

    Damon


    Damon Bu - MSFT
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, July 25, 2013 6:00 AM
  • This is not about "code" - I asked about the the .NET DataGridView RowValidating event firing when the form loads and if there was a workaround for when there are existing rows of data that fail validation tests - it seems odd to me to have this fire on load - normally validation events fire on user interaction.  But nevertheless, I have worked it out.  All better now.

    "Microsoft never has bugs, just creative features!", an old IT sage...


    Newark IT Guy...

    Thursday, July 25, 2013 9:11 AM
  • Hi,

    First of all,I'm sorry for misunderstanding you.

    I'm glad to hear that you got it working.

    If you can share your solutions & experience here, it will be very beneficial for other community members who have similar questions. Thank you !

    Regards,

    Damon


    Damon Bu - MSFT
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, July 25, 2013 9:48 AM
  • Damon,

    Alright.  First, I put a public static bool in the form class as thus:

          public static bool loading = true;

    At the top of any DataGridView Row validating event, I have put this:

           if( loading == true)
            return;

    Then. at the end of the form load event, I placed:

           loading = false;

    This bypasses any validation on form load, but allows the code to function thereafter.


    Newark IT Guy...

    Thursday, July 25, 2013 10:12 AM
  • Hi,

    Thank you for sharing your solution  here. It will be very beneficial for other community members who have similar questions.

    Have a nice day!

    Damon


    Damon Bu - MSFT
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, July 26, 2013 1:12 AM