Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8891465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:45:11+00:00 2026-06-14T22:45:11+00:00

I am working on a Winforms app that displays a DataGridView, bound to some

  • 0

I am working on a Winforms app that displays a DataGridView, bound to some database table.

It allows inserting fresh entries in to it, and does some data validation.

When a required column is left blank, or one of the unique constraints is violated, the DataError event calls this function:

protected void _data_error(object sender, DataGridViewDataErrorEventArgs e)
{
    MessageBox.Show(this,e.Exception.Message,"Error");
    e.ThrowException = false;
    e.Cancel = false;
}

When the popup is closed, the new row that was being edited is removed.
When this is done to a row that has already been saved (an update operation) the row loses its changes and loses focus.
I assume this means I need to signal the application to keep the row editable, but I do not know how to do that.

Paradoxically, if I replace the event handler with a throw(e.Exception) the exception gets thrown to the wind and is picked up by the uncaught exception handler, but the new row is preserved after that window closes.

How can I preserve the new row on a DataError event?

Edit:

My next thought was to save the row and add it to the DataGridView’s data source after the MessageBox pops up.
That does not work because adding data to the data source add’s it as a committed row which throws an exception because of the invalid data vs keeping the data as an editable row so that validation doesn’t happen.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:45:12+00:00Added an answer on June 14, 2026 at 10:45 pm

    Took me a few days but here is how I fixed it, but I am still open for a better way.

    In the DataGridView.RowValidating event, validate the contents of each cell.
    If it is invalid do this:

    Grid.Rows[e.RowIndex].ErrorText = "Concisely describe the error and how to fix it";
    e.Cancel = true;
    

    And make sure you clear out the error text on the next trip through the event handler.

    For the case when a user is entering an invalid type of data, text into a numerical only column for instance, you will have to handle the error in a DataGridView.DataError event. Use the same code:

    Grid.Rows[e.RowIndex].ErrorText...
    

    but there is no need to clear the ErrorText as your row validation event will handle that.

    Note: This will not allow you to have popups to inform the user what is wrong, it will use the DataGridView error, which is a red exclamation mark on the left hand side of the grid that has mouseover text displaying the ErrorText you filled. It appears anytime you have a MessageBox popup you will lose focus on the editable data and lose the row.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataGridView control in a winforms app that I'm working on. The
I'm working on a simple WinForms app that uses an .sdf database to store
I'm working with a WinForms app. I have an RDLC report that will be
I'm working with a winForms app that has a listView. That listView has multi-select
I am working on an asp.net (or winforms) app that is supposed to detect
I'm working on a .NET WinForms app that needs to print a FEDEX shipping
I am working on a WinForms app that uses the ReportViewer control to show
I am working on a winforms app and I have added some controls dynamically
When I have a WinForms app that needs to do some heavy lifting in
I'm working on WinForm app in VS2008, I have a sql table that is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.