Solved

How do you handle data validation (OnRowValidating) in a DataGridView

Posted on 2009-07-08
3
577 Views
Last Modified: 2012-05-07
If a user leaves a record in a DataGridView and they haven't entered a required field I keep getting system.Data.NoNullAllowedException. ..
I'm trying to catch it with the code below but it doesn't work.  Is there an easy way to do this?

Thanks,
Private Sub myDataGridView_RowValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles myDataGridView.RowValidating

        Try

        Catch ex As NoNullAllowedException

            MessageBox.Show("You left a record without filling in all of the required fields " & ex.Message)

        End Try

    End Sub

Open in new window

0
Comment
Question by:FatalErr
3 Comments
 
LVL 15

Expert Comment

by:JackOfPH
Comment Utility
0
 
LVL 13

Accepted Solution

by:
drypz earned 500 total points
Comment Utility
Hi! Try this...
        private void Form2_Load(object sender, EventArgs e)

        {

            FillGridView();

        }
 

        private void FillGridView()

        {

            dataGridView1.Columns.Add("Column1", "Name");

            dataGridView1.Columns.Add("Column2", "Age");

        }
 

        private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)

        {

            if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].IsInEditMode)

            {

                if (string.IsNullOrEmpty(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue.ToString()))

                {

                    MessageBox.Show("Please enter value.");

                    e.Cancel = true;

                }

            }
 

        }

Open in new window

0
 
LVL 1

Author Comment

by:FatalErr
Comment Utility
I ended up using the DataError event which seems to be designed for what I needed.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

11 Experts available now in Live!

Get 1:1 Help Now