Ecyrb
Ecyrb

Reputation: 2080

How can I display an error to the user while they're still editing a DataGridView cell?

If a user enters invalid text in a DataGridView's cell I want to prevent the user from leaving that cell and display an error icon in that cell with an error message in a tooltip. It seems that normally the error icon won't appear until the cell is no longer in edit mode. I found an example of how to get the error icon to appear while it's still in edit mode, but the tooltip won't show up when I hover over it.

I'm using Windows 7, but I couldn't get it to work in XP either, so we can rule that out.

Can anybody figure out what the example is missing or suggest an alternative approach that would yield the same results?

To get the example working you'll need to create your own form, copy/paste the example code, add using System.Drawing.Drawing2D;, and assign the event handlers yourself.

Upvotes: 2

Views: 1104

Answers (2)

thegman121
thegman121

Reputation: 71

Its a little hack-y, but simply adding something like this to the end of your validation method would work (assuming your view isn't bound to some data so that EndEdit() submits a new value to a data structure or something):

if (e.Cancel)
{
    myView.EndEdit();
    myView.BeginEdit(true);
}

Upvotes: 1

Jacob Seleznev
Jacob Seleznev

Reputation: 8131

I suggest you look at IDataErrorInfo interface

For example see the following post.

Upvotes: 1

Related Questions