Data Validation On AddNewRow

7 posts, 1 answers
  1. Fish
    Fish avatar
    4 posts
    Member since:
    May 2019

    Posted 22 Oct Link to this post

    Hello, I have some question when I'm doing data validation.

    Too big to attch, Please see gif on https://drive.google.com/file/d/1EVPmQXUDcdogYR6cas_nQZztAwCMySD8/view

    As my url, I hope can add rows only if column2 has value.

    It's work perfect when I'm clicking other space area.

    But when I'm clicking Close Button, It's will fired RowValidating and AddRow at the same time, and I can't closed this form by close button.

    Is there anything I doing wrong? Or there have other solution to let me solve this problem?

    Thanks for your assistance.

     
  2. Nadya
    Admin
    Nadya avatar
    105 posts

    Posted 24 Oct Link to this post

    Hello Fish,

    The provided information is greatly appreciated. I suppose that you want to make some data validation and you use the RowFormatting event. Actually, this is the expected behavior of using the event when validation fails, you are not allowed to close the form. Thus, it is guaranteed that all cells are valid.

    If you want to have data validation when adding a new row and be able to close the form I can suggest you to use the UserAddingRow event which will fire for the new rows that are going to be added. Calling the MasterView.TableAddNewRow.CancelAddNewRow method will allow you to discard the changes in the new row and close the form. You can use the CellValidating event for the rows that contain data. The main difference with the RowValidating event is that the cell editor will be kept active until the user enters a valid value if the CellValidating event is cancelled. However, the RowValidating event allows you to commit invalid values to the DataBoundItem and the validation will be performed whenever you want to leave this row. That is why the CellValidating event may be more appropriate if you want to ensure that no invalid data is submitted to the data objects. It allows you to reject the editor's value as if the Escape key is pressed by calling the GridViewElement.CancelEdit method while the grid is in edit mode. Could you please give that approach a try and see how it works for you?

    However, if you want to use the RowValidating event please take a look at the following code snippet:

    bool close = true;
    private void RadForm1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (close)
        {
            close = false;
            radGridView1.MasterView.TableAddNewRow.CancelAddNewRow();
            this.Close();
        }
      
    }
    
    private void RadGridView1_RowValidating(object sender, RowValidatingEventArgs e)
    {
        if (e.Row.Cells["FirstName"].Value + "" == "")
        {
            e.Cancel = true;
        }
    }
    

    Feel free to use this approach which suits your requirements best.

    I hope this information helps. If you need any further assistance please don't hesitate to contact me.

    Regards,
    Nadya
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  3. Fish
    Fish avatar
    4 posts
    Member since:
    May 2019

    Posted 24 Oct in reply to Nadya Link to this post

    Hi Nadya,

    Thanks for your reply.

    And I am still have problem, hope you can answer me again.

    1. CellValidating 

        => It's good, But I have more than one column need filling.

    2. UserAddingRow

        => It's also good, But I have a lot of column, when doing CancelAddNewRow , it cause other column value disappear.

    So, I decide to use RowValidating, And I have try the code which you provide.

    GIF : https://drive.google.com/file/d/1BS1lQ3x9hU5_hWNNQemDIaRDIRu90xnp/view

    It's still added row and I can't close. Have I misunderstand?

    And most important thing is can I skip added Row?  Because I am writting insert SQL in UserAddedRow, and In Table schema column2 and column3 is not allow value is null, It's will cause Exception.

    Thanks for your helping!

  4. Answer
    Nadya
    Admin
    Nadya avatar
    105 posts

    Posted 28 Oct Link to this post

    Hello Fish,

    The RowValidating event fires for all rows in the grid and it is suitable especially for the data rows. When validation on adding a new row is required the most suitable approach is to use the UserAddingRow event. This event ensures that no invalid values should be added to the database in the UserAddedRow event. If I understand your requirement correctly you want to be able to close the form even if the validation fails. This is why I suggest the CancelAddNewRow method which discards the changes in the new row and closes the form. It is normal behavior when closing the form to lose the submitted in new row data when validation fails.

    I created a test project for reference. The result is demonstrated in the attached gif file. Could you please check it and let me know how it works for you? If this does not satisfy your custom requirement I would kindly ask you to describe in more detail which scenarios you would like to cover when performing data validation.

    I am looking forward to your reply.

    Regards,
    Nadya
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  5. Fish
    Fish avatar
    4 posts
    Member since:
    May 2019

    Posted 29 Oct in reply to Nadya Link to this post

    Hello Nadya,

    Thank you for your explanation, It's work perfect now.

    And I have last question, as my attach gif(addingrow), UserAddingRow will active when I filled any column.

    But now, if I don't want to create row anymore, how can I skip UserAddingRow?(Hope it's like attach file : no_active_addrow.gif)

    I've tried specific all column null value, but it's doesn't work.

    Thanks for your helping!
    My code:

    private void MasterTemplate_UserAddingRow(object sender, GridViewRowCancelEventArgs e)
    {
        if (e.Rows[0].Cells["column2"].Value + "" == "" || e.Rows[0].Cells["column3"].Value + "" == "")
        {
            e.Cancel = true;
     
            var confirmClear = RadMessageBox.Show("Cancel adding ?", "Confirm", MessageBoxButtons.YesNo);
            if (DialogResult.Yes == confirmClear)
            {
                foreach (GridViewCellInfo column in e.Rows[0].Cells)
                {
                    column.Value = null;
                }
            }
        }
    }
  6. Nadya
    Admin
    Nadya avatar
    105 posts

    Posted 30 Oct Link to this post

    Hello Fish,

    Following your description, I suppose that you want to reject adding the new row when some condition is met. In this case, I can suggest using the CancelAddNewRow method. Note that e.Cancel in the UserAddingRow event will prevent the incorrect data to be saved in the grid while calling the MasterView.TableAddNewRow.CancelAddNewRow method will discard the changes in the current row and will reject the adding new row operation as well. Please refer to the following code snippet:

    private void RadGridView1_UserAddingRow(object sender, GridViewRowCancelEventArgs e)
    {
        if (e.Rows[0].Cells["Column1"].Value + "" == "" || e.Rows[0].Cells["Column2"].Value + "" == "")
        {
            e.Cancel = true;
    
            var confirmClear = RadMessageBox.Show("Cancel adding ?", "Confirm", MessageBoxButtons.YesNo);
            if (DialogResult.Yes == confirmClear)
            {
                radGridView1.MasterView.TableAddNewRow.CancelAddNewRow();
            }
        }
    }
    

    I hope this helps. Should you have any other questions do not hesitate to ask.

     Regards,
    Nadya
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  7. Fish
    Fish avatar
    4 posts
    Member since:
    May 2019

    Posted 31 Oct Link to this post

    Hello Nadya,

    Yes, You're right, It's work perfect.

    I appreciate your help very much.

Back to Top