?
Solved

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

Posted on 2009-07-08
3
Medium Priority
?
601 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24810053
0
 
LVL 13

Accepted Solution

by:
drypz earned 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

777 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