Solved

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

Posted on 2009-07-08
3
580 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
ID: 24810053
0
 
LVL 13

Accepted Solution

by:
drypz earned 500 total points
ID: 24810422
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
ID: 25388788
I ended up using the DataError event which seems to be designed for what I needed.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

896 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

17 Experts available now in Live!

Get 1:1 Help Now