CancelCurrentEdit in DataGrid

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a simple form with a datagrid, an OK button and a Cancel button.

If the user presses the Cancel button, the form should close immediately, no
matter the state of the datagrid.
When the user edits the datagrid, leaves a cell blank which doesn't allow
null values and presses the Cancel button, a messagebox appears:

"Column <columnname> does not allow nulls. Do you want to correct the
value?"

How can I cancel the form without displaying this messagebox?
 
Is this a winforms grid? If so, I believe the Validating event can be
overriden and you can just cancel it there.
 
Thanks for your response!

Yes, it's a Windows forms project.
However, I don't think overriding the Validating event will work. The event
is triggered before the Cancel code executes. If I want to do this using
some kind of override, I have to tell the Validate event the difference
between a normal validation or a cancel event.

Can you tell me how I can do such a thing?

Regards,

Ruben
 
Hello Cor,

No, this doesn't work because the null validation triggers before the Click
event of the Cancel button.

Please see my other post on how I solved this.

Regards,

Ruben
 
Hi Marina,

Thanks for your suggestion, but it was one of the first things I tried.
However, the null validation still fires.

Please see my other post on how I solved this.

Regards,

Ruben
 
OK, I solved it, but it's not a very elegant solution.
In the constructor method of the form, I changed all the necessary columns
(AllowDBNull = true).

To check for a Cancel, I created a global form variable:
private bool _cancelEdit = false;

In the MouseEnter event of the Cancel button, the _cancelEdit variable is
set to true, the MouseLeave events turns it back to false.

To enable null validation on the table, I created my own RowChanging event:

if (!_cancelEdit && (e.Action == DataRowAction.Add || e.Action ==
DataRowAction.Change))
{
string[] fields = {"Gebruikersnaam", "Wachtwoord", "Naam"};

foreach (string field in fields)
{
if (e.Row[field] == DBNull.Value || (string)e.Row[field] == String.Empty)
{
throw new Exception("The field '" + field + "' is a required field.\n");
}
}
}

I hope Visual Studio 2005 / NET 2.0 has a more elegant datagrid class for
this kind of situations.

Regards,

Ruben
 
Ruben,

I did not really investigate your method because that I do not see the
complete impact of your problem. (Not needed to explain if you are happy so
far).

Moreover because as answer on your question included in your answer,
especially the datagrid in the version 2.0 is very much improved. Things
that were hard to do with a datagrid are now easily possible with a
property.

Have a look at this already old preview.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvsdev05/html/vs05a9.asp

Ik hoop dat het iets bijdraagt

Cor
 
Back
Top